I generally write something like this in my Rspec tests:
user.new(...)
user.should be_valid
The problem is, when that test fails, I don't get to see the errors on the user object. Is there a nice way to re-write this test so that in the Rspec output I'll see something like user.errors.inspect
? I've tried user.errors.should be_empty
, but that still just says "expected true, got false."
You can do this by defining a custom matcher. Something like this should do the trick.
RSpec::Matchers.define :be_valid do
match do |actual|
actual.valid?
end
failure_message_for_should do |actual|
"expected that #{actual} would be valid (errors: #{actual.errors.full_messages.inspect})"
end
failure_message_for_should_not do |actual|
"expected that #{actual} would not be valid"
end
description do
"be valid"
end
end
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