5

我班里的逻辑有时会Rollbar.silenced忽略一些异常(所以它们不会被报告)。

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

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在报告未获救的错误时不使用 in :error:critical等方法。:warning

我看到 report_exception_to_rollbarcall_with_rollbar里面的 rollbar源代码Rollbar.scoped.

所以我尝试使用以下方法对其进行测试:

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

但它也告诉我:

 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 进行测试?

4

2 回答 2

3

您要确保运行的行是exception_reporter.rb 中的 Rollbar.log 调用。( Rollbar.error,Rollbar.warning等只是 包装Rollbar.log。)

尝试这个:

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

于 2015-09-11T17:11:27.883 回答
0

log对我不起作用,但error确实如此。这是 Rails 应用程序中的 ApplicationJob 示例。

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

于 2021-02-26T02:16:45.200 回答