Rspec的匹配器不使用自定义错误消息
问题描述:
我有一个自定义匹配:Rspec的匹配器不使用自定义错误消息
RSpec::Matchers.define :have_value do |attribute, expected|
match do |obj|
obj.send(attribute) == expected
end
description do
"have attribute #{attribute} with value #{expected}"
end
end
这是我如何使用它的例子:
context "description" do
subject { create_obj_from_file(file_name) }
h = {
:attribute1 => 6,
:attribute2 => 3,
:attribute3 => "PL" }
}
h.each do |k,v| it { should have_value k, v} end
end
这是正常运行我的测试。但是,当我得到一个错误,这不是自定义错误,它是“预期{masssive对象转储}有价值:atttribute和值”任何想法,我什么我做错了?
答
感谢您使用响应你的最后一个问题,我的代码以下是你需要什么,这个例子:
failure_message_for_should do |obj|
"should have value #{expected} for attribute #{attribute} but got #{obj.send(attribute)}"
end
failure_message_for_should_not do |obj|
"should not have value #{expected} for attribute #{attribute} but got #{obj.send(attribute)}"
end
答
您需要指定自定义失败消息从the wiki一个例子:。
RSpec::Matchers.define :be_a_multiple_of do |expected|
match do |actual|
actual % expected == 0
end
failure_message_for_should do |actual|
"expected that #{actual} would be a precise multiple of #{expected}"
end
failure_message_for_should_not do |actual|
"expected that #{actual} would not be a precise multiple of #{expected}"
end
description do
"be a precise multiple of #{expected}"
end
end
谢谢主席先生,检查是邮件 :) – 2011-04-21 04:59:44