作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个自定义匹配器,它在它的匹配 block 中使用期望(这里的代码被简化了)
RSpec::Matchers.define :have_foo_content do |expected|
match do |actual|
expect(actual).to contain_exactly(expected)
expect(actual.foo).to contain_exactly(expected.foo)
end
end
expected collection contained: ["VLPpzkjahD"]
actual collection contained: ["yBzPmoRnSK"]
the missing elements were: ["VLPpzkjahD"]
the extra elements were: ["yBzPmoRnSK"]
expected MyObject to have_foo_content "foobar"
failure_message do |actual|
# ...
end
最佳答案
你可以抢救RSpec::Expectations::ExpectationNotMetError
在您的 match
捕获失败的期望消息:
match do |object|
begin
expect(object).to be_nil
rescue RSpec::Expectations::ExpectationNotMetError => e
@error = e
raise
end
end
failure_message do
<<~MESSAGE
Expected object to meet my custom matcher expectation but failed with error:
#{@error}
MESSAGE
end
不要忘记在
rescue
中重新加注, 否则不起作用
关于ruby - 在 rspec 自定义匹配器中重用失败消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58447648/
我是一名优秀的程序员,十分优秀!