I have a Rails 6.1 controller that sets:
# File: app/controllers/foo_controller.rb
rescue_from Exception, with: :error_handler
My Rspec 6.1 test (is a "request" type) is like:
# File: spec/requests/foo_controller_spec.rb
expect do
post foo_path, params: wrong_params
end.to raise_error(Exception, 'Non valid params')
That type of tests don't work because "rescue_from" blocks the exception call. I see there is the bypass_rescue option, but it works only for the Rspec controller type test. I tried adding to my test:
# File: spec/requests/foo_controller_spec.rb
module BypassRescue
def rescue_with_handler(exception)
raise exception
end
end
# Extends the controller with a module that overrides
# `rescue_with_handler` to raise the exception passed to it. Use this to
# specify that an action _should_ raise an exception given appropriate
# conditions.
def bypass_rescue
controller.extend(BypassRescue)
end
And then:
# File: spec/requests/foo_controller_spec.rb
expect do
bypass_rescue
post foo_path, params: wrong_params
end.to raise_error(Exception, 'Non valid params')
But it didn't work. So, my question is, should I abandon the "raise_error(Exception" option? or there is way to test the exception dodging the "rescue_from" method?
I have a Rails 6.1 controller that sets:
# File: app/controllers/foo_controller.rb
rescue_from Exception, with: :error_handler
My Rspec 6.1 test (is a "request" type) is like:
# File: spec/requests/foo_controller_spec.rb
expect do
post foo_path, params: wrong_params
end.to raise_error(Exception, 'Non valid params')
That type of tests don't work because "rescue_from" blocks the exception call. I see there is the bypass_rescue option, but it works only for the Rspec controller type test. I tried adding to my test:
# File: spec/requests/foo_controller_spec.rb
module BypassRescue
def rescue_with_handler(exception)
raise exception
end
end
# Extends the controller with a module that overrides
# `rescue_with_handler` to raise the exception passed to it. Use this to
# specify that an action _should_ raise an exception given appropriate
# conditions.
def bypass_rescue
controller.extend(BypassRescue)
end
And then:
# File: spec/requests/foo_controller_spec.rb
expect do
bypass_rescue
post foo_path, params: wrong_params
end.to raise_error(Exception, 'Non valid params')
But it didn't work. So, my question is, should I abandon the "raise_error(Exception" option? or there is way to test the exception dodging the "rescue_from" method?
Share Improve this question edited Feb 14 at 0:18 aarkerio asked Feb 14 at 0:11 aarkerioaarkerio 2,3642 gold badges22 silver badges38 bronze badges 1 |1 Answer
Reset to default 0This works:
# File: spec/requests/foo_controller_spec.rb
module ActionController
module Rescue
def rescue_with_handler(exception)
raise exception
end
end
end
describe FooController, type: :request do
describe '#raise_exception' do
it 'is the payload is not a valid one' do
expect do
post inbound_messages_provider_path(:bandwidth), params: { foo: :bar }, headers: @headers
end.to raise_error(Exception, 'Non valid BW params. Spam.')
end
end
end
Exception
as it's the parent class of all exceptions including stuff likeSignalException::Interrupt
which you should not prevent. Only rescue specific exception classes that you know what to do with and leave the high level exception handling on the framework level. This code will just bury bugs and make debugging exceptionally hard. Get rid of it. – max Commented Feb 14 at 7:23