Search code examples
rubytddrspecxunit

How to attach a message to RSpec check?


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'

Solution

  • For RSpec 3+:

    The message could be customized as a string or using a proc(check the reference).

    expect(1).to eq(2), 'one is not two!'
    

    Customized message RSpec tries to provide useful failure messages, but for cases in which you want more specific information, you can define your own message right in the example. This works for any matcher other than the operator matchers.

    source @ relishapp


    For older RSpec versions

    should and should_not take a second argument (message) that overrides the matcher’s default message.

    1.should be(2), 'one is not two!'
    

    The default messages are usually pretty useful though.