Rails 3 seems is ignoring my rescue_from handler so I cannot test my redirect below.
class ApplicationController < ActionController::Base
rescue_from ActionController::RoutingError, :with => :rescue_404
def rescue_404
flash[:notice] = "Error 404. The url <i>'#{env["vidibus-routing_error.request_uri"]}'</i> does not exist on this website."
redirect_to root_path
end
end
In both functional and integration tests, this rescue_from is ignored, and the error is raised:
ActionController::RoutingError: No route matches "/non_existent_url"
test/integration/custom_404_test.rb:5:in `test_404'
How can I make sure this is properly 'caught' in a test?
Rails 3 handles ActionController::RoutingError
in middleware, so ApplicationController::rescue_from
doesn't see the exception. The Rails core team recommends using a catch-all route at the bottom of routes.rb
(GitHub issue) until they decide on a fix.
One option is to use a catch-all route to handle routing errors then manually raise an exception to hit rescue_from
(code from my blog post about this issue):
# routes.rb
match "*path", :to => "application#routing_error"
# application_controller.rb
rescue_from ActionController::RoutingError, :with => :render_not_found
def routing_error
raise ActionController::RoutingError.new(params[:path])
end
def render_not_found
render :template => "misc/404"
end