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
How to Express Infinity in Ruby
How to Generate Links with Trailing Slash in Rails 3
Handling Exceptions Raised in a Ruby Thread
To_Specs': Could Not Find Chef (>= 0) Amongst [] (Gem::Loaderror)
Converting a Hexadecimal Digest to Base64 in Ruby
Bundle Install Issue with Libv8 and Rails
How to Make 'Whenever' Gem Work on Windows
Ruby: What Is the Easiest Way to Remove the First Element from an Array
How to Test Exception Raising in Rails/Rspec
How to Copy File Across Buckets Using Aws-S3 or Aws-Sdk Gem in Ruby on Rails
What Exactly "Config.Assets.Debug" Setting Does
Ruby Forgets Local Variables During a While Loop
Is There a Cucumber Hook to Run Before and After Each Feature
Replace "&" to "\&" in Ruby Seems Impossible
Language in a Sandbox in Rails
Rspec --Init Not Working/ 'Mkd Ir': Invalid Argument - ./C: (Errno::Einval)