1

私は自分でカスタムマッチャーを作成しましたが、それ自体は正常に機能します。しかし、failure_message_for_shouldは機能しません。それでもデフォルトの失敗メッセージが表示されます。...should_not作品!

私のマッチャー:

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/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.
4

1 に答える 1

2

ここで起こっていることは、assert_dom_equalメソッドがを返す代わりに例外を発生させることだと思いますfalse。RSpecは例外をキャッチし、マッチャーメッセージの代わりに例外メッセージを返します。

あなたはこれを自分で捕まえることができるはずです:

match do |actual|
  begin
    assert_dom_equal(expected, actual)
  rescue MiniTest::Assertion
    false
  end
end
于 2011-02-14T03:18:32.327 に答える