-
-
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
Presence validations which have an if condition on related models seem broken #1198
Comments
Hey, so I took a closer look at the backtrace and it looks like the failure is coming from AssociationMatcher, i.e. |
I think I hit the same or a similar problem here: sodabrew/puppet-dashboard#388 There are two tests which are now failing but if I downgrade shoula-matcher to ~> 3 they pass again. |
In my case I bisected the issue to commit 3af3d9f . It also seems like a |
@ZeroPointEnergy Yup, you got it — it will now check to make sure that the record fails (or doesn't fail) validation if the association is set to nil. Thanks for a link to your repo, I'll take a closer look at it. |
Closing this issue because of missing context for the original problem posted. |
We've fixed the issue in our project, it was technically an issue with our code even though it was exposed by shoulda-matchers 4.x. I wanted to explain how we encountered this for anyone else who might stumble upon it. Consider a model such as: class Wheel
belongs_to :car
validates_presence_of :colour, if: :car_requires_coloured_wheels?
def car_requires_coloured_wheels?
car.requires_coloured_wheels?
end
end On the face of it, the condition on the presence validation seems fine because However, that's misguided. While the model will refuse to save unless the wheel belongs to a car, the model can still be validated independently of that, which is what So when shoulda-matchers is running our The fix is for our validation condition to not assume the association exists; something like def car_requires_coloured_wheels?
return false unless car
car.requires_coloured_wheels?
end Thanks @mcmire and @ZeroPointEnergy for your work and help with this. |
After upgrading from 3.1.3 to 4.0.1, I've found that two of our tests that use shoulda-matchers have started to fail. Both of these tests are asserting that a model validates presence of an attribute, as well as including an
if
condition for whether to run the validation, that accesses related models.Suppose we have two models,
User
andGroup
, whereby a user belongs to a group and a group has many users. Additionally, aGroup
belongs to aProvider
, which is used to determine some behaviour of Groups and Users that use that provider. Given that, I have a presence validation for an identifier onUser
that is only required if the group is using a particular provider:Which is tested with:
In 3.1.3, this test passed. In 4.0.1, I now get:
Initially I thought this was a factory_bot change or bug, as it seems like the associations of the
User
object created by the factory haven't been created. However, by downgrading solely the shoulda-matchers gem, the tests pass again, which leads me to believe this is likely a change in the 4.x version of shoulda-matchers.Ruby version: 2.6.1
Rails version: 5.2.3
shoulda-matchers gem version: 4.0.1
factory_bot gem version: 5.0.2
factory_bot_rails gem version: 5.0.1
The text was updated successfully, but these errors were encountered: