Last Updated: February 25, 2016
·
1.922K
· david

Tweak that RSpec error message

Your tests should make it as easy as possible for you to understand why they failed. Custom matchers help you achieve that, but sometimes they are overkill. If all you need is to tweak the error message just a little bit, you can pass a second argument in your expectation. So, let's say you have this code:

expect(a).to have_something

When the test fails, you will see the following:

Failure/Error: expect(a).to have_something
  expected #has_something? to return true, got false

You can change it to

expect(a).to have_something, "Nooo!"

And when the test fails, you will see the following, instead:

Failure/Error: expect(a).to have_something, "Nooo!"
  Nooo!

1 Response
Add your response

This is awesome, still can't believe this was always there. Wow :)

over 1 year ago ·