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 回答