How to Test Exception Raising in Rails/Rspec

Rspec: How to test a method that raises an error

You should be able to use Rspec compound expectations for this

https://relishapp.com/rspec/rspec-expectations/docs/compound-expectations

So I'll re-write your expectation to something like this:

expect { handler.call }.
to raise_error(FailedPaymentError).
and change { Subscription.count }.by(1)

Rspec: How to test an exception is raised in private method?

try and_raise(Product::StaleObjectError.new(nil, nil))

see this question i asked a while about the same issue:

Rspec - wrong number of arguments when raising error

Testing methods that eventually throw an exception in RSpec

You can expect errors to be raised in code. In order for the exception to be caught by RSpec, you need to use a block as follows:

it 'records the failure with our error reporting service' do
expect(ErrorReporter).to receive(:increment).with(
'bad_request',
source: {
account_id: 1
},
sporadic: true
)

expect { available_channels.fetch }
.to raise_error RemoteService::InvalidRequest
end


Related Topics



Leave a reply



Submit