gpt4 book ai didi

ruby-on-rails - Rspec 测试 Sentry 的 Raven capture_exception

转载 作者:太空宇宙 更新时间:2023-11-03 17:28:49 30 4
gpt4 key购买 nike

假设我有这段代码...

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

我尝试使用 expect(Raven).to receive(:capture_exception).with(...) 并且无论我如何分割它,我似乎都无法将 expect 绑定(bind)到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 spy :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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com