Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to write tests for 500 error page in Rails?

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?

like image 281
concept47 Avatar asked Jun 27 '12 03:06

concept47


2 Answers

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
like image 153
concept47 Avatar answered Oct 13 '22 04:10

concept47


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
like image 36
alf Avatar answered Oct 13 '22 03:10

alf