I have a custom error pages in my rails app testing the 404 error seems straight forward enough (get nonexistent page and do assert_match/select for certain text) but I'm wondering how to test the 500 error page.
Any ideas?
So what I found out was that I could do something like this in rspec
def other_error
raise "ouch!"
end
it "renders 500 on Runtime error" do
get :other_error
response.should render_template("errors/500")
response.status.should == 500
end
Here's what I do, assuming you're using rspec, rspec-mocks and capybara:
First, you need to find a controller action that calls a method. For example, you might have a UserController
with a show
action that calls User.find
. In that case, you can do something like this:
it "should render the 500 error page when an error happens" do
# simulate an error in the user page
User.should_receive(:find).and_raise("some fancy error")
visit '/user/1'
# verify status code
page.status_code.should eql(500)
# verify layout
page.title.should eql('Your site title')
page.should have_css('navigation')
page.should have_css('.errors')
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With