Skip to content

Cannot call rspec nilify_blanks on STI models #37

@armchairdj

Description

@armchairdj

I changed one of my tables into an STI table so I could subclass my Post model into Article, Review and MicroBlog. Immediately, the following test started failing:

describe "nilify_blanks" do
  it { is_expected.to nilify_blanks(before: :validation) }
end

This despite the following in my ApplicationRecord class:

class ApplicationRecord < ActiveRecord::Base
  ...
  nilify_blanks before: :validation
  ...
end

At first I thought it was because of some Rails internal doing something strange to the type attribute because it's STI. But then I discovered this works:

describe "nilify_blanks" do
  # Must specify individual fields for STI models.
  it { is_expected.to nilify_blanks_for(:alpha,   before: :validation) }
  it { is_expected.to nilify_blanks_for(:body,    before: :validation) }
  it { is_expected.to nilify_blanks_for(:slug,    before: :validation) }
  it { is_expected.to nilify_blanks_for(:summary, before: :validation) }
  it { is_expected.to nilify_blanks_for(:title,   before: :validation) }
  it { is_expected.to nilify_blanks_for(:type,    before: :validation) }
end

So I can use the nilify_blanks_for matcher on the STI type field and every other text/string field in the model. But when I use the nilify_blanks matcher, boom.

This is the failure before my change:

1) Post concerns behaves like an_application_record included nilify_blanks should nilify blanks [:before, :validation] and [:types, [:string, :text, :citext]]
   Failure/Error: it { is_expected.to nilify_blanks(before: :validation) }
     expected to nilify blanks {:before=>:validation, :types=>[:string, :text, :citext]}

This is a bummer because I went from being able to test nilify_blanks in a shared example for ApplicationRecord that's included in every model spec, to having to test it individually in just this one random model. The nilify_blanks matcher still works on all my non-STI models.

Any thoughts?

Thanks so much for an amazing gem that I use on every project.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions