Sign in through GitHub

Please read for an updated status on RailsCasts:

Learn more or hide this

Robert Bradford's Profile

GitHub User: robertwbradford

Site: http://robertwbradford.com

Comments by Robert Bradford

Avatar

You may want to ask this on Stackoverflow. Then you can provide some more details about your setup.

(If you haven't figured it out already :)

Avatar

This doesn't work for controller specs :( Any ideas on how to get this working for controller specs?

Avatar

The answer is here: http://stackoverflow.com/questions/13996259/testing-error-pages-in-rails-with-rspec-capybara

ruby
# /config/environments/test.rb
consider_all_requests_local = false
config.action_dispatch.show_exceptions = true
Avatar

Did you find a good way to do this? For example, using this episode, ActiveRecord::RecordNotFound errors ultimately redirect to the errors/404 view. However, I have the following in one of my tests (Item belongs_to List):

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

However, the test fails with the following:

ruby
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. Hmm...

Avatar

Thanks, @Xavar. I had been using the following:

ruby
raise ActionController::RoutingError

But because I wasn't calling .new('Not Found') on it, I was getting a 500 (missing arguments) instead of the expected 404. So as a lesson, you can use either of the following to trigger a 404:

ruby
raise ActiveRecord::RecordNotFound
# or
raise ActionController::RoutingError.new(:not_found)
Avatar

I'm sure this is simple, but I can't find a way to manually send someone to a 404 (using the middleware approach). For example, I want to "hide" private widgets if the widget author is not the current user:

ruby
class ThingsController < ApplicationController
  def show
    @thing = Thing.find(:id)
    if @thing.user != current_user && @thing.is_private?
      # What do I put here to fake a 404?
    end
  end
end

I tried render text: "Not found", status: 404 but that does exactly that, just renders the text instead of handling it through the errors controller. I also tried render status: 404, but that still shows the record (albeit with a 404 status in the web inspector).

Avatar

Same thing happened to me when watching this and Episode #343.

Avatar

Hey Adam, I just ran into this as well. Were you able to find the reason or a solution? Thanks.