Rspec测试Sentry的Raven capture_exception

Chr*_*ugh 1 ruby rspec ruby-on-rails sentry

假设我有这段代码...

  Raven.capture_exception(error, {
    extra: {
      error_message: message
    }
  })
end
Run Code Online (Sandbox Code Playgroud)

我已经尝试使用expect(Raven).to receive(:capture_exception).with(...),无论如何切片,我似乎都无法将期望绑定到Raven,因此可以验证它是否已发送日志通信。它一直告诉我capture_exception没有定义。我都尝试过expect并且expect_any_instance_of没有运气。现在,我已经跳过了,但是我知道有办法。有什么想法吗?

Car*_*nez 6

不确定您要精确测试什么,但这对我有用:

class Test
  def self.test
    begin
      1 / 0
    rescue => exception
      Raven.capture_exception(exception)
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

在测试中,我可以设置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
Run Code Online (Sandbox Code Playgroud)