假设我有这段代码。。。

  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

关于ruby-on-rails - Rspec测试Sentry的Raven capture_exception,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54831920/

10-13 07:04