5

Logic inside my class sometimes uses Rollbar.silenced to ignore some exception (so they don't get reported).

I'm trying to write a test which ensure that rollbar actually report error.

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

Unfortunately rollbar doesn't use in :error, :critical, :warning etc. methods when reporting unrescued errors.

I saw report_exception_to_rollbar and call_with_rollbar inside rollbar sourcecode which are wrapped with Rollbar.scoped.

So I tried to test it with:

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

but it also told me:

 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

How do I ensure that the exception is caught by rollbar and test it with rspec?

Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
Filip Bartuzi
  • 5,711
  • 7
  • 54
  • 102

2 Answers2

3

The line you want to ensure runs is the Rollbar.log call in exception_reporter.rb. (Rollbar.error, Rollbar.warning, etc. are just wrappers around Rollbar.log.)

Try this:

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

Brian Rue
  • 126
  • 3
  • already tried that. does not work. ` expected: 1 time with any arguments received: 0 times with any arguments` – Filip Bartuzi Sep 11 '15 at 17:15
  • error is for 100% raised because `expect { query }.to raise_error(unknown_exception)` passes. Do you think it might be connected with test envorinment? – Filip Bartuzi Sep 11 '15 at 17:16
  • Ah, could be because Rollbar is disabled in the test environment. The standard initializer does that: https://github.com/rollbar/rollbar-gem/blob/master/lib/generators/rollbar/templates/initializer.rb#L16-L19 You could try removing those lines from your config/initializers/rollbar.rb , or set `config.enabled = true` before running this particular test. – Brian Rue Sep 11 '15 at 17:23
  • `Rollbar.configuration.enabled` is `=> true` however still no calls to `:log` method – Filip Bartuzi Sep 11 '15 at 17:46
  • also: with Rollbar enabled I probably don't want to `.and_call_original` anymore :) – Filip Bartuzi Sep 11 '15 at 17:58
0

log didn't work for me, but error did. Here is an example with ApplicationJob in a Rails app.

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

Dudo
  • 4,002
  • 8
  • 32
  • 57