假设我有这段代码...
Raven.capture_exception(error, {
extra: {
error_message: message
}
})
end
我尝试使用expect(Raven).to receive(:capture_exception).with(...)
,无论如何切片,我似乎都无法将期望绑定到Raven,因此可以验证它是否已发送日志通信。它总是告诉我capture_exception
没有定义。我尝试过expect
和expect_any_instance_of
都没有运气。现在,我已经跳过了,但是我知道有办法。有想法吗?
答案 0 :(得分:5)
不能完全确定要精确测试什么,但这对我有用:
class Test
def self.test
begin
1 / 0
rescue => exception
Raven.capture_exception(exception)
end
end
end
在测试中,我可以设置RSpec间谍:https://relishapp.com/rspec/rspec-mocks/docs/basics/spies
it 'calls raven capture_exception' do
allow(Raven).to receive(:capture_exception) # Setup the spy
Test.test # Call the function that uses Raven.capture_exception
expect(Raven).to have_received(:capture_exception) # Check that the spy was called
end