我想检查一种方法的结果:
result.should == [1.0,2.0,3.0]
但我收到一个错误:
expected: [1.0, 2.0, 3.0]
got: [1.0, 2.0, 3.0] (using ==)
我认为问题在于四舍五入,但我不知道如何比较它们,例如偏差为0.1。
谢谢你,呼吸。
我写了自己的匹配器,如果它对某人有帮助:
RSpec::Matchers.define :be_closed_array do |expected, truth|
match do |actual|
same = 0
for i in 0..actual.length-1
same +=1 if actual[i].round(truth) == expected[i].round(truth)
end
same == actual.length
end
failure_message_for_should do |actual|
"expected that #{actual} would be close to #{expected}"
end
failure_message_for_should_not do |actual|
"expected that #{actual} would not be close to #{expected}"
end
description do
"be a close to #{expected}"
end
end
使用(已弃用)2.7.0 https://github.com/rspec/rspec-expectations/commit/4638609046a120c86d48a0253b471db55a5fd441#diff-a26c79378299c0868d0462d2000c54e204e9b854f1dd772c05e7deb588033ff0R3,删除于3.0.0 https://github.com/rspec/rspec-expectations/commit/a00be5b15dacfae7aa0be68fdd4ccd1ebd2ddf1a#diff-4ac680eeaffa2461320261649ec69d2b543331a37dc7ac88c33de1f77163293c):
.should be_close
Or even:
.should be_within
参考这里http://rubydoc.info/gems/rspec-expectations/2.4.0/RSpec/Matchers http://rubydoc.info/gems/rspec-expectations/2.4.0/RSpec/Matchers
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)