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 404end
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...
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)
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
classThingsController < ApplicationControllerdefshow@thing = Thing.find(:id)
if@thing.user != current_user && @thing.is_private?
# What do I put here to fake a 404?endendend
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).
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 :)
This doesn't work for controller specs :( Any ideas on how to get this working for controller specs?
The answer is here: http://stackoverflow.com/questions/13996259/testing-error-pages-in-rails-with-rspec-capybara
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):
However, the test fails with the following:
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...
Did you find a good way of doing this?
Thanks, @Xavar. I had been using the following:
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: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:
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 triedrender status: 404
, but that still shows the record (albeit with a 404 status in the web inspector).Same thing happened to me when watching this and Episode #343.
Hey Adam, I just ran into this as well. Were you able to find the reason or a solution? Thanks.