【发布时间】:2017-11-14 20:59:48
【问题描述】:
我有一些 RSpec 的代码。 我的(或不是我的)班级可以(或不)调用特定的异常。 例如,类::Foo::Bar::TimeOutError。 我要检查一下。
describe :describe_description do
before :all do
object = Class.new
# method can be call Class::Foo::Bar::TimeOutError
object.method
end
it :exception do
expect {}.to_not raise_error Class::Foo::Bar::TimeOutError
end
end
这段代码有效,但 RSpec 写了一个警告:
警告:使用
expect { }.not_to raise_error(SpecificErrorClass)有误报的风险,因为实际上任何其他错误都会导致期望通过,包括 Ruby 引发的错误(例如 NoMethodError、NameError 和 ArgumentError),这意味着您打算测试的代码甚至可能不会到达。而是考虑使用expect {}.not_to raise_error或expect { }.to raise_error(DifferentSpecificErrorClass)。可以通过设置来抑制此消息:RSpec::Expectations.configuration.on_potential_false_positives = :nothing。
如何修复我的代码以使 RSpec 正常工作且不发出警告?
【问题讨论】:
-
当您预期某种错误时,传递特定的错误类很有用。没有明显的理由来测试不引发特定错误,因为您不希望在执行期间引发任何错误。
expect {}.to_not raise_error可能是最好的解决方案。