Rspec测试Sentry的Raven capture_exception

问题描述 投票:1回答:1

假设我有这段代码......

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

我试图使用expect(Raven).to receive(:capture_exception).with(...),无论我如何切片,我似乎无法绑定对Raven的期望,所以我可以验证它是否发送了日志记录通信。它一直告诉我capture_exception没有定义。我试过没有运气的expectexpect_any_instance_of。现在,我已经跳过这个,但我知道有办法。思考?

ruby-on-rails ruby rspec sentry
1个回答
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
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.