jeudi 4 février 2021

Rails tests skipping custom validations or custom validation not working?

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.

Aucun commentaire:

Enregistrer un commentaire