我能够测试是否已调用[C0],但是在也会引发错误的方法中调用它时,它会因为引发错误而失败,并且会跳过Rollbar.warning
。
Rollbar.warning
这是我正在测试的方法:
context 'unauthorized' do
before do
allow(Rollbar).to receive(:warning)
end
it 'sends a warning to rollbar' do
subject.send(:log_and_raise_error)
expect(Rollbar).to have_received(:warning)
end
end
但是当我运行规格时,它失败:
def log_and_raise_error
Rollbar.warning(
"Not authorized error accessing resource #{ResourceID}"
)
raise NotAuthorized
end
关于如何解决该错误并仍然测试Rollbar的任何想法?
您可以期待该错误或对其进行挽救:
期待错误:
1) Authorization unauthorized sends a warning to rollbar
Failure/Error: subject.send(:log_and_raise_error)
NotAuthorized
救援错误:
it 'sends a warning to rollbar' do
expect { subject.send(:log_and_raise_error) }.to raise_error NotAuthorized
expect(Rollbar).to have_received(:warning)
end
或
it 'sends a warning to rollbar' do
subject.send(:log_and_raise_error) rescue NotAuthorized
expect(Rollbar).to have_received(:warning)
end