Shoulda-matchers: Test the model to validate attribute ONLY on specific context, except default ones

Created on 18 Sep 2016  路  8Comments  路  Source: thoughtbot/shoulda-matchers

I need to write failing test for activerecord model. It ensures, that a validation runs ONLY on specific context, and not on default ones. I wrote:

it { is_expected.not_to validate_presence_of(:address).on(:create).on(:update) }

it { is_expected.to validate_presence_of(:address).on(:save) }

But it throws me an error:

NoMethodError:
undefined method `attribute_setter' for nil:NilClass
# /Users/molfar/.gem/ruby/2.3.1/gems/shoulda-matchers-3.1.1/lib/shoulda/matchers/active_model/allow_value_matcher.rb:405:in `failure_message'

How to figure out this situation?

Bug

Most helpful comment

There's no workaround for this, unfortunately -- it's a fatal flaw in how I ended up designing matchers-within-matchers. I had started work on this last year but put it on hold because it ended up ballooning into a pretty substantial change. Now that I've had some time to focus on other things, I've started to really dig into this again. There's light at the end of the tunnel but it will be some more time until I reach an end for this. I think it will be worth it, though. Thanks for your patience.

All 8 comments

This may be related to #904 -- I'll have to investigate this for sure.

Regardless, though, the matcher is failing for some reason. Perhaps your model doesn't have the presence validation in question?

Model has validation

validates :address, presence: true

Any progress on this? It's been a year and I'm still getting it.

Same here, has anyone found the problem or a valid work around?

I'm also having this problem.

Same here - any news or workaround?

There's no workaround for this, unfortunately -- it's a fatal flaw in how I ended up designing matchers-within-matchers. I had started work on this last year but put it on hold because it ended up ballooning into a pretty substantial change. Now that I've had some time to focus on other things, I've started to really dig into this again. There's light at the end of the tunnel but it will be some more time until I reach an end for this. I think it will be worth it, though. Thanks for your patience.

Thanks a lot for the update. 馃挭 and 馃嵑

Was this page helpful?
0 / 5 - 0 ratings