rails
rails copied to clipboard
Make the test environment show rescuable exceptions in responses
Background
During integration tests, it is desirable for the application to respond as closely as possible to the way it would in production. This improves confidence that the application behavior acts as it should.
In Rails tests, one major mismatch between the test and production
environments is that exceptions raised during an HTTP request (e.g.
ActiveRecord::RecordNotFound
) are re-raised within the test rather
than rescued and then converted to a 404 response.
Setting config.action_dispatch.show_exceptions
to true
will make the
test environment act like production, however, when an unexpected
internal server error occurs, the test will be left with a opaque 500
response rather than presenting a useful stack trace. This makes
debugging more difficult.
This leaves the developer with choosing between higher quality integration tests or an improved debugging experience on a failure.
I propose that we can achieve both.
Solution
Change the configuration option config.action_dispatch.show_exceptions
from a boolean to one of 3 values: :all
, :rescuable
, :none
. The
values :all
and :none
behaves the same as the previous true
and
false
respectively. What was previously true
(now :all
) continues
to be the default for non-test environments.
The new :rescuable
value is the new default for the test environment.
It will show exceptions in the response only for rescuable exceptions as
defined by ActionDispatch::ExceptionWrapper.rescue_responses
. In the
event of an unexpected internal server error, the exception that caused
the error will still be raised within the test so as to provide a useful
stack trace and a good debugging experience.