Q: 404, 500 error page testing using RSpec + Capybara.

2,105 views
Skip to first unread message

Douglas Yun

unread,
Jun 1, 2012, 3:23:20 PM6/1/12
to pdxruby-beginners
Hey PDX,

I'm wondering if there was an easier way of testing custom 404 & 500
error pages during development. I've created the pages, and they work
during development, however I have to comment out
'Rails.application.config.consider_all_requests_local' from my
ApplicationController and routes.rb for them to work. My /environment/
development.rb is set with 'config.consider_all_requests_local =
true'.

Moreover, what is the recommended approach to writing an acceptance
test for these error pages?

---------

Here's my ApplicationController:

class ApplicationController < ActionController::Base
# ...

unless Rails.application.config.consider_all_requests_local
rescue_from Exception, with: :render_500
rescue_from ActionController::RoutingError, with: :render_404
rescue_from ActionController::UnknownController, with: :render_404
rescue_from AbstractController::ActionNotFound, with: :render_404
rescue_from ActiveRecord::RecordNotFound, with: :render_404
end

private
def render_404(exception)
@not_found_path = exception.message
respond_to do |format|
format.html { render template: 'errors/error_404', layout:
'layouts/application', status: 404 }
format.all { render nothing: true, status: 404 }
end
end

def render_500(exception)
@error = exception
respond_to do |format|
format.html { render template: 'errors/error_500', layout:
'layouts/application', status: 500 }
format.all { render nothing: true, status: 500}
end
end

# ...
end

----------

Here's my routes.rb

unless Rails.application.config.consider_all_requests_local
match '*not_found', to: 'errors#error_404'
end

---------

Specifically, I'm following the guidance from this blog post.
http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-500-error-pages

Please let me know if I should clarify anything else.

Best,

Doug

Sam Livingston-Gray

unread,
Jun 4, 2012, 5:43:11 PM6/4/12
to pdxruby-...@googlegroups.com
Hi, Doug-

I was a bit distracted by the .consider_all_requests_local thing, but
of course that makes sense -- normally, you want to see the exception
stack trace in development. I don't recall what the default for this
setting is in the test environment, but you might consider changing
the check to something like "unless Rails.env.development?" so that
your tests exhibit the behavior that you'd like to test.

...which is what your question was actually about.

Honestly, how much testing you do for this depends on how important
the 404 and 500 functionality is to you. I might not bother writing
tests for this at all, but if you're being thorough, this seems like a
decent minimum (using ActionController::TestCase and Mocha):

test "has a 404 page" do
get '/utter_rubbish'
assert_response 404
assert_template 'path/to/404-template'
end

test "has a 500 page" do
HeadController.expects(:asplode).raises(OMGWTFBBQ_Exception)
get '/head/asplode'
assert_response 500
assert_template 'path/to/500-template'
end

...if there's more interesting behavior, of course, elaborate as necessary.

Hope this helps,
-Sam

Douglas Yun

unread,
Jun 11, 2012, 3:47:27 PM6/11/12
to pdxruby-beginners
Hey Sam,

What a great explanation for the 500 page! Haha. I did want to be
thorough and did need the practice of writing tests.

Thanks for taking the time to do this write up.

-Doug
> >http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-...
Reply all
Reply to author
Forward
0 new messages