[rspec-users] issue with rescue_action_in_public! and testing 404 responses

128 views
Skip to first unread message

christofferklang

unread,
Aug 8, 2010, 3:53:26 PM8/8/10
to rspec...@rubyforge.org
Hello,

I'm new to rails and I'm trying to wrap my heads around how to spec controllers using RSpec (using rails 3rc1 and rspec 2.0.0.beta.19).

The problem I've run into is when I want to test that my controllers respond with a 404 for unfound records. Whenever I run the spec, the ActiveRecord::RecordNotFound exception is not caught by rails, causing the spec example to fail since the exception propagates out of the controller into the spec.

>From googling around I get that rails only handles the exceptions and does a 404 response when it's run in production mode, and that you need to call rescue_action_in_public! in your example if you want this behavior for test mode. However, this does not seem to do anything for me. The example still fails because the exception bubbles escapes unhandled from the controller.

Do I need to set something up for the rescue_action_in_public! to work? Or is this not the correct way to test missing records?

My full example: (using factory_girl, rspec mocks and devise)


it "respons with 404 when trying to edit non-existing reads" do
   rescue_action_in_public!
   sign_in(@user)
   Read.should_receive(:find_by_id_and_user_id!).with(2, @user.id).and_raise(ActiveRecord::RecordNotFound)

   get :edit, :id => 2
   response.status.should eql 404
 end


and the exception:
1) ReadsController resources respons with 404 for non existing reads for GET /reads/2/edit
    Failure/Error: get :edit, :id => 2
    ActiveRecord::RecordNotFound
Thanks, /Christoffer

View this message in context: issue with rescue_action_in_public! and testing 404 responses
Sent from the rspec-users mailing list archive at Nabble.com.

David Chelimsky

unread,
Aug 9, 2010, 10:21:40 PM8/9/10
to rspec-users
On Aug 8, 2010, at 2:53 PM, christofferklang wrote:

Hello,

I'm new to rails and I'm trying to wrap my heads around how to spec controllers using RSpec (using rails 3rc1 and rspec 2.0.0.beta.19).

The problem I've run into is when I want to test that my controllers respond with a 404 for unfound records. Whenever I run the spec, the ActiveRecord::RecordNotFound exception is not caught by rails, causing the spec example to fail since the exception propagates out of the controller into the spec.

>From googling around I get that rails only handles the exceptions and does a 404 response when it's run in production mode, and that you need to call rescue_action_in_public! in your example if you want this behavior for test mode. However, this does not seem to do anything for me. The example still fails because the exception bubbles escapes unhandled from the controller.

Do I need to set something up for the rescue_action_in_public! to work? Or is this not the correct way to test missing records?

My full example: (using factory_girl, rspec mocks and devise)

it "respons with 404 when trying to edit non-existing reads" do
   rescue_action_in_public!
   sign_in(@user)
   Read.should_receive(:find_by_id_and_user_id!).with(2, @user.id).and_raise(ActiveRecord::RecordNotFound)

   get :edit, :id => 2
   response.status.should eql 404
 end


and the exception:
1) ReadsController resources respons with 404 for non existing reads for GET /reads/2/edit
    Failure/Error: get :edit, :id => 2
    ActiveRecord::RecordNotFound
It's up to you to handle the error in the controller. Something like this in ApplicationController:

  rescue_from ActiveRecord::RecordNotFound do
    render '/404.html', :layout => false, :status => :not_found
  end

HTH,
David

Thanks, /Christoffer

John Firebaugh

unread,
Aug 10, 2010, 4:52:16 PM8/10/10
to rspec...@rubyforge.org
On Aug 9, 7:21 pm, David Chelimsky <dchelim...@gmail.com> wrote:
> It's up to you to handle the error in the controller. Something like this in ApplicationController:
>
>   rescue_from ActiveRecord::RecordNotFound do
>     render '/404.html', :layout => false, :status => :not_found
>   end

Actually, I believe this is a bug in Rails 3.
ActiveRecord::RecordNotFound should be handled automatically by the
ActionDispatch::ShowExceptions middleware, and then
rescue_action_in_public! in your controller test should continue to
work as it did in Rails 2, allowing you to verify the result is a 404.

I posted to rails-core about this issue:

http://groups.google.com/group/rubyonrails-core/browse_thread/thread/e7379309d2308f28
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply all
Reply to author
Forward
0 new messages