首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >确保使用RSpec时Rollbar已报告错误

确保使用RSpec时Rollbar已报告错误
EN

Stack Overflow用户
提问于 2015-09-12 00:52:49
回答 2查看 1.7K关注 0票数 5

我正在尝试编写一个测试,以确保rollbar确实报告错误。

代码语言:javascript
复制
it 'does not mute rollbar' do
        expect(Rollbar).not_to receive(:silenced)
        expect(Rollbar).to receive(:error).with(any_args).and_call_original
        expect { query }.to raise_error(unknown_exception)
end

不幸的是,在报告未挽救的错误时,rollbar不能在:error:critical:warning等方法中使用。

我在rollbar sourcecode中看到了report_exception_to_rollbarcall_with_rollbar,它们都是用Rollbar.scoped包装的。

因此,我尝试使用以下命令进行测试:

expect(Rollbar).to receive(:scoped).with(any_args).and_call_original

但它也告诉我:

代码语言:javascript
复制
 Failure/Error: expect(Rollbar).to receive(:scoped).with(any_args).and_call_original
       (Rollbar).scoped(*(any args))
           expected: 1 time with any arguments
           received: 0 times with any arguments

如何确保异常被rollbar捕获并使用rspec进行测试?

EN

回答 2

Stack Overflow用户

发布于 2015-09-12 01:11:28

您希望确保运行的行是Rollbar.log call in exception_reporter.rb。(Rollbar.errorRollbar.warning等只是Rollbar.log的包装器。)

试试这个:

expect(Rollbar).to receive(:log).with(any_args).and_call_original

票数 3
EN

Stack Overflow用户

发布于 2021-02-26 10:16:45

log不适合我,但error适合。下面是一个在Rails应用中使用ApplicationJob的示例。

代码语言:javascript
复制
describe "ApplicationJob" do
  describe "rollbar" do
    context "when rollbar is included" do
      before do
        class EasyJob < ApplicationJob
          def perform(*)
          end
        end

        allow_any_instance_of(EasyJob).to receive(:perform).and_raise(StandardError) # rubocop:disable RSpec/AnyInstance
      end

      it "makes a call to rollbar" do
        expect(Rollbar).to receive(:error).with(any_args)
        expect { EasyJob.perform_now }.to raise_error(StandardError)
      end
    end

    context "when rollbar is NOT included" do
      before do
        class HardJob < ActiveJob::Base
          def perform(*)
          end
        end

        allow_any_instance_of(HardJob).to receive(:perform).and_raise(StandardError) # rubocop:disable RSpec/AnyInstance
      end

      it "does NOT make a call to rollbar" do
        expect(Rollbar).not_to receive(:error)
        expect { HardJob.perform_now }.to raise_error(StandardError)
      end
    end
  end
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32528479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档