I get this error when running my rspec expects with custom validators.
expect {@ua.save!}.to raise_error(ActiveRecord::RecordInvalid,'Validation failed: This question is no longer active')
fails with
expected ActiveRecord::RecordInvalid with "Validation failed: This question is no longer active", got #<ActiveRecord::RecordInvalid: Validation failed: This question is no longer active.> with backtrace:
This only seems to be problem with my custom validations. See this model:
class UserAnswer < ActiveRecord::Base
belongs_to :user
belongs_to :question
validate :questionIsActive?
private
def questionIsActive?
errors.add(:base, "This question is no longer active.") if !self.question.is_active?
end
end
Using: Rails 3.2.11 Rspec-rails 2.12.2
You have a typo. Add a period to your string:
expect {@ua.save!}.to raise_error(ActiveRecord::RecordInvalid,'Validation failed: This question is no longer active.')
Note: you currently have in your expectation:
'Validation failed: This question is no longer active'
but need:
'Validation failed: This question is no longer active.'
so that it matches your validation string:
'This question is no longer active.'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With