How to test render status: 404 with Rails4 and RSpec when using rescue_from
Another approach with RSpec 3+ would be to test for an exception:
it 'responds with a 404 if the page is not found' do
expect { get(:show, id: 'bad_id') }.to raise_error(ActionController::RoutingError)
end
Although I'm not very happy with this solution, at least this is a work around:
I splited the test into two separate specs. One for testing the response code 404 (with GET instead of visit) and a second to test the alert. The second test is necessary because get
doesn't render the view - even if render_views
is defined on top of the spec-file.
it 'response with 404 if page not found' do
get :show, { controller: 'pages', id: 'not_existing_page_321' }
expect(response.status).to eq(404)
end
it 'renders an error-message if page not found and shows index' do
visit page_path('page_not_found')
within '.alert-error' do
page.should have_content("Page page_not_found doesn't exist")
end
end