In rspec: Can I attach a message to a check the same way as I would do in xUnit style test frameworks? How?
assert_equal value1, value2, "something is wrong"
In rspec: Can I attach a message to a check the same way as I would do in xUnit style test frameworks? How?
assert_equal value1, value2, "something is wrong"
should
andshould_not
take a second argument (message
) that overrides the matcher’s default message.The default messages are usually pretty useful though.
update:
for RSpec 3:
In my case it was a problem of parenthesis:
this resulted in a wrong number of arguments, while the correct way is:
In RSpec, it's the matcher's job to print a sensible failure message. The generic matchers that ship with RSpec can obviously only print generic non-descript failure messages, since they don't know anything about your particular domain. That's why it is recommended that you write your own domain-specific matchers, which will give you both more readable tests and more readable failure messages.
Here's an example from the RSpec documentation:
Note: only
match
is required, the others will be generated automatically. However, the whole point of your question is of course that you do not like the default messages, so you need to at least also definefailure_message_for_should
.Also, you can define
match_for_should
andmatch_for_should_not
instead ofmatch
if you need different logic in the positive and negative case.As @Chris Johnsen shows, you can also explicitly pass a message to the expectation. However, you run the risk of losing the readability advantages.
Compare this:
with this:
That would (roughly) be implemented like this: