Displaying error messages in RSpec failures
April 23, 2008
The suggested way of checking if an ActiveRecord model has errors with RSpec is:
@something.errors.should be_empty
When that fails, you get a report that says:
expected empty? to return true, got false
That’s not very helpful because it doesn’t tell you what the errors are. If you do this:
@something.errors.full_messages.should == []
You get a more informative failure message:
expected: [],
got: ["Whatever can't be blank"] (using ==)
Comments
1.
Or you could use my ValidationSpecHelper module, which could be called like so:
and return failure messages like so:
and so on...
http://svn.ekenosen.net/public/rspec_validation_matcher/
There are other validation spec matchers out there, but I like mine the best. ;-)
2.
Oh well, I guess the
3.
Or you just give up on rspec and its spaghetti mess of code and start using shoulda.
should_not_allow_values_for :email, "blah", "b lah"
should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
Comments Disabled