require_relative '../price_entry_class.rb'
require_relative '../exceptions.rb'
require 'spec_helper'
describe PriceEntry do
it "throws expection on invalid date format" do
expect { described_class.date_from_input_to_storage("20010101") }.to raise_error(InvalidInputData)
expect { described_class.date_from_input_to_storage("zażółć") }.to raise_error(InvalidInputData)
expect { described_class.date_from_input_to_storage(nil) }.to raise_error(InvalidInputData)
expect { described_class.date_from_input_to_storage(-19_199) }.to raise_error(InvalidInputData)
end
end在一个Rspec测试中有多个期望是可以的吗?rubocop-rspec声称最好将这个测试分为4:
spec/price_entry_class_spec.rb:14:2: C: Example has too many expectations [4/1].声称在每个单元测试中拥有单一断言总是更好,这对我来说是相当令人惊讶的。有充分的理由把这样的测试分成四个单独的测试吗?
这个测试一次只测试一件事情(显然是格式错误的输入,导致异常)。
我可以想象把它改写成
["20010101", "zażółć", nil, -19_199].each do |malformed_input|
expect { described_class.date_from_input_to_storage(malformed_input) }.to raise_error(InvalidInputData)
end但我不相信它正在提高可读性、可维护性或其他任何东西。
发布于 2017-07-14 18:34:11
原因是,如果这个测试失败了,您将不得不分别确定4个子预期中哪个失败了。
根据测试报告的输出,这可能是一个实际问题,也可能不是一个实际问题。
这个测试一次只测试一件事情(显然是格式错误的输入,导致异常)。
实际上,你在测试:
nil引发错误也就是说,如果你实际上是在做手动模糊化,而不关心这些区别--尽管我认为它们在这个特殊的情况下是有用的--那么我看不出这有什么问题。
测试规则和最佳实践是有帮助的,但不要陷入其中。如果你知道你自己的测试目标,并且明白为什么偏离最佳实践可能是可以的,那么一定要这么做。
https://codereview.stackexchange.com/questions/169262
复制相似问题