-
-
Notifications
You must be signed in to change notification settings - Fork 909
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
How to prevent NoMethodError in validation which have an if statement ? #1524
Comments
Hi @guppy0356, The short answer is yes, you still have to check for the presence of You can think about this two ways. First, Second, even if there were not a validation on Hope that helps. |
Thank you for your respond @mcmire, Hmm, your answer is my sample code is prefer to early return, isn't it? |
@guppy0356 Yeah -- or some kind of logic at least. You could also use the def car_requires_coloured_wheels?
car&.requires_coloured_wheels?
end |
@mcmire |
Late to the party here, but have been banging my head against a wall with this issue. Note: I'm going to intentional not comment or add opinions on whether associations should be tested. Or whether it's good to have validations that rely on values from other records… sometimes we have what we have! 😂 But I'm not overly keen on approaches that use a guard clause, or It could be solved with:
Obviously this is a lot more verbose and goes against using the gem consistently though. But thought I'd leave this here in case it helps someone out. I sort of understand why |
Me again! Another solution…for when you do want to use I think this should work in your scenario!
|
I saw #1198 and I faced same issue too. Do we really have to use early return in the method? This is depends on shouda-matcher not depends on its spec.
Even if I set up the association correctly using subject, it is still nil in the test. How to prevent NoMethodError in testing. Is there nothing I can do? Early return is NOT good method to avoid this.
Appendix
The text was updated successfully, but these errors were encountered: