gpt4 book ai didi

rspec - 将更多信息附加到 RSpec 的默认失败消息?

转载 作者:行者123 更新时间:2023-12-05 00:31:40 27 4
gpt4 key购买 nike

我在这样的验证中测试了很多坏字符串:

['0', '3:a', 'xx:11', '-1', '-3:00', 'h', 'h2', 'h2h', 'm', 'm10', 'm10m', '2hm', 'h2m', 'hm2', '2m10h', '2m10m', '2h10h'].each do |input|
FactoryGirl.build(:time_record, duration_string: input).should have(1).error_on('duration_string'), "Tested value was '#{input}'"
end

可悲的是,当其中一个失败时,消息 expected 1 error on :duration_string, got 0没有告诉我,哪个失败了。我知道我可以传递显示的第二个参数,而不是像这样的默认消息:
x.should have(1).error_on('duration_string'), "Tested value was '#{input}'"

但这隐藏了原始消息。有没有办法只将我自己的消息附加到原始消息而不是替换它?

谢谢你。

最佳答案

您可以使用另一条消息捕获并重新引发异常:

['0', '3:a', 'xx:11', '-1', '-3:00', 'h', 'h2', 'h2h', 'm', 'm10', 'm10m', '2hm', 'h2m', 'hm2', '2m10h', '2m10m', '2h10h'].each do |input|
begin
FactoryGirl.build(:time_record, duration_string: input).should have(1).error_on('duration_string'), "Tested value was '#{input}'"
rescue RSpec::Expectations::ExpectationNotMetError => e
e.message << "failed at #{input}"
raise e
end
end

关于rspec - 将更多信息附加到 RSpec 的默认失败消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14379941/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com