1

Using this Railscast episode, ActiveRecord::RecordNotFound errors ultimately redirect to the ErrorsController and the /app/views/errors/404.html.erb view. However, I have the following in one of my tests (Item belongs_to List):

scenario "item cannot be created without a list" do
  visit new_item_path
  # Because it's not the proper nested new_list_item_path(@some_list) path,
  # @list is not created in the ItemsController.
  page.should have_content('Not Found')
  page.status_code.should be 404
end

However, the test fails with the following:

Failure/Error: visit new_item_path
ActiveRecord::RecordNotFound:
  Couldn't find List without an ID

So the controller is working properly in requiring a list, but the test fails because it doesn't take into account the custom exception handling which should send the user to the 404.

How can I properly test for the redirect?

robertwbradford
  • 6,181
  • 9
  • 36
  • 61
  • looks like you didn't create test data beforehand?Are you using FactoryGirl or Machinist for scenario? – kasperite Mar 21 '14 at 03:13
  • @kasperite, I'm using FactoryGirl. I didn't create any test data (e.g., a list), because this test is to see if it properly shows the 404 error because there is no list. – robertwbradford Mar 21 '14 at 03:17
  • ah I see :), this SO might give you some hints I think: http://stackoverflow.com/questions/13996259/testing-error-pages-in-rails-with-rspec-capybara. – kasperite Mar 21 '14 at 03:24
  • @kasperite, ............................................................................. is a good thing :) Thank you! I promise I searched for other questions first but didn't see that one. – robertwbradford Mar 21 '14 at 04:00
  • sweet, glad it helped – kasperite Mar 21 '14 at 04:06
  • @kasperite, thanks for your help before. I noticed that this solution isn't working for *controller* specs. Any ideas? http://stackoverflow.com/questions/22623862/rspec-controller-spec-incorrectly-failing-when-using-custom-exception-handling-f – robertwbradford Mar 25 '14 at 01:40

0 Answers0