【问题标题】:Rspec testing Sentry's Raven capture_exceptionRspec 测试 Sentry 的 Raven capture_exception
【发布时间】:2019-07-16 20:00:00
【问题描述】:

假设我有这段代码...

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

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

【问题讨论】:

  • 你想存根Raven.capture_exception,对吧?
  • 听起来您正在尝试测试 gem 的基本功能,但为什么呢?看来你不信任它。
  • 我正在尝试测试 Sentry 正在被使用,并且消息的格式符合预期

标签: ruby-on-rails ruby rspec sentry


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 2017-03-13
    • 2021-10-22
    • 2017-12-31
    • 2014-11-13
    • 2013-10-04
    相关资源
    最近更新 更多