【发布时间】:2019-04-06 02:44:33
【问题描述】:
我有一个要测试的函数在输入时引发异常,但该异常还包含比普通消息更多的信息,我也想测试它。所以我做了这样的事情as seen in the rspec documentation:
it 'raises the correct exception' do
expect { my_call }.to raise_error do |error|
expect(error.some_field).to eq('some data')
end
end
这很好用,但它与RSpec/MultipleExpectations cop 发生冲突:
RSpec/MultipleExpectations: Example has too many expectations [2/1]
据我所知,不可能像这样以块的形式使用raise_error,而不需要一个以上的期望,那又是什么呢?有什么方法可以在示例之外以某种方式保存引发的异常,以便我可以正常指定它,而不会在规范中做一些涉及rescue 的可怕事情?还是应该使用自定义的raise_custom_error 匹配器?
【问题讨论】: