Rspec 测试哨兵乌鸦 capture_exception

Rspec testing Sentry's Raven capture_exception

假设我有这段代码...

  Raven.capture_exception(error, {
    extra: {
      error_message: message
    }
  })
end

我已尝试使用 expect(Raven).to receive(:capture_exception).with(...),但无论我如何分割它,我似乎都无法将 expect 绑定到 Raven,因此我无法验证它是否已发送日志通信。它一直告诉我 capture_exception 未定义。我试过 expectexpect_any_instance_of 都没有成功。现在,我已经跳过了这个,但我知道有办法。想法?

不完全确定你到底想测试什么,但这对我有用:

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