I have a model Subscription
with a boolean attribute :fee
which should not be modified after the creation of a Subscription
.
I added a custom validation like this:
validate :fee_not_changed
def fee_not_changed
return unless created_at != updated_at && fee_changed?
errors.add :fee, :changed, message: "cannot be changed"
end
Our API is not allowing us to update :fee
(the error is returned) but I can update the attribute in tests despite the custom validation like so:
test "fee cant be edited after creation" do
subscription = subscriptions(:with_fee)
subscription.update!(fee: false)
assert_not subscription.saved_change_to_fee?
end
Expected true to be nil or false
Also, if I put a byebug
before the assertion, I can see that it effectively updates the attribute skipping the custom validation.
What's the problem? Thanks for any help provided.
question from:
https://stackoverflow.com/questions/66049180/rails-tests-skipping-custom-validations-or-custom-validation-not-working 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…