Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test the model to validate attribute ONLY on specific context, except default ones #963

Closed
molfar opened this issue Sep 18, 2016 · 8 comments · Fixed by #1136
Closed

Test the model to validate attribute ONLY on specific context, except default ones #963

molfar opened this issue Sep 18, 2016 · 8 comments · Fixed by #1136

Comments

@molfar
Copy link

molfar commented Sep 18, 2016

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?

@molfar molfar changed the title Test the model NOT to validate attribute on specific context Test the model to validate attribute ONLY on specific context, except default ones Sep 18, 2016
@mcmire
Copy link
Collaborator

mcmire commented Sep 19, 2016

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?

@molfar
Copy link
Author

molfar commented Sep 19, 2016

Model has validation

validates :address, presence: true

@stevenspiel
Copy link

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

@00dav00
Copy link

00dav00 commented Nov 15, 2017

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

@nickqw12
Copy link

I'm also having this problem.

@dgilperez
Copy link

Same here - any news or workaround?

@mcmire
Copy link
Collaborator

mcmire commented Aug 29, 2018

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.

@dgilperez
Copy link

Thanks a lot for the update. 💪 and 🍺

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants