failure_message_for_should doesn't work

401 views Asked by At

I wrote myself a custom matcher, which itself works fine. But the failure_message_for_should doesn't work, I still get the default failure message. The ...should_not works!

My matcher:

RSpec::Matchers.define :be_same_dom do |expected|
  match do |actual|
    assert_dom_equal(expected, actual)
  end

  failure_message_for_should do |actual|
    "Expected the same DOM as #{expected}, but got #{actual}"
  end

  failure_message_for_should_not do |actual|
    "Expected a different DOM than #{expected}"
  end
end

Failure messages:


Failure/Error: helper.link_to("Dictionary", dictionaries_path).should_not be_same_dom('<a href="/dictionaries">Dictionary</a>')
       Expected a different DOM than <a href="/dictionaries">Dictionary</a>

Failure/Error: helper.link_to("Dictionary", dictionaries_path).should be_same_dom('<a class="x" href="/dictionaries">Dictionary</a>')
     MiniTest::Assertion:
       <"<a class=\"x\" href=\"/dictionaries\">Dictionary</a>"> expected to be == to
       <"<a href=\"/dictionaries\">Dictionary</a>">..
       Expected block to return true value.
1

There are 1 answers

0
zetetic On BEST ANSWER

I think what's happening here is that the assert_dom_equal method raises an exception instead of returning false. RSpec catches the exception and returns the exception message instead of the matcher message.

You should be able to catch this yourself:

match do |actual|
  begin
    assert_dom_equal(expected, actual)
  rescue MiniTest::Assertion
    false
  end
end