Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ensuring Rollbar has reported error with RSpec

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?

like image 394
Filip Bartuzi Avatar asked Oct 20 '22 02:10

Filip Bartuzi


1 Answers

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

like image 134
Brian Rue Avatar answered Oct 22 '22 00:10

Brian Rue