deegee
2008-Jul-25 10:21 UTC
Nothing raised in functional test if exception is recued in controller?
Hi, I''m puzzled why my functional tests are failing. I have the following test: assert_raise ActiveRecord::RecordInvalid do post :create, :page_id => id_from_fixture, :body => "bla bla" end assert_redirected_to my_error_path In my controller, I am catching the error: def create begin @page = current_user.pages.find(params[:page_id]) do_some_stuff rescue ActiveRecord::RecordInvalid redirect_to my_error_path end end When I run the test, it fails on the grounds that ''nothing was raised''. But I know for sure that the error was raised, because when I remove the assert_raise test and just check the redirect, the test passes. Is it because I already rescue in the controller that the assertion doesn''t see the error anymore? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ganesh Gunasegaran
2008-Jul-25 10:53 UTC
Re: Nothing raised in functional test if exception is recued in controller?
You have handled the RecordInvalid exception in the controller itself. Once you have handled the exception you cannot test it using assert_raise Ex below: -------------------------------------------------------------------- def boom begin raise Exception.new("Blah") rescue Exception => e puts "Exception Caught " + e end end require "test/unit" class MyTest < Test::Unit::TestCase def test_boom assert_raise Exception do boom end end end -------------------------------------------------------------------- gg@ubuntu:~/Work/Ruby$ ruby gg.rb Loaded suite gg Started Exception Caught Blah F Finished in 0.06206 seconds. 1) Failure: test_boom(MyTest) [gg.rb:13]: <Exception> exception expected but none was thrown. 1 tests, 1 assertions, 1 failures, 0 errors -------------------------------------------------------------------- Try the same test without handling the exception -------------------------------------------------------------------- def boom begin raise Exception.new("Blah") #rescue Exception => e # puts "Exception Caught " + e end end require "test/unit" class MyTest < Test::Unit::TestCase def test_boom assert_raise Exception do boom end end end -------------------------------------------------------------------- gg@ubuntu:~/Work/Ruby$ ruby gg.rb Loaded suite gg Started . Finished in 0.002276 seconds. 1 tests, 1 assertions, 0 failures, 0 errors -------------------------------------------------------------------- Cheers, Ganesh Gunasegaran. SageWork - Simplify IT(http://www.sagework.com) On Fri, 2008-07-25 at 03:21 -0700, deegee wrote:> Hi, > I''m puzzled why my functional tests are failing. I have the following > test: > assert_raise ActiveRecord::RecordInvalid do > post :create, :page_id => id_from_fixture, :body => "bla bla" > end > assert_redirected_to my_error_path > > In my controller, I am catching the error: > def create > begin > @page = current_user.pages.find(params[:page_id]) > do_some_stuff > rescue ActiveRecord::RecordInvalid > redirect_to my_error_path > end > end > > When I run the test, it fails on the grounds that ''nothing was > raised''. But I know for sure that the error was raised, because when I > remove the assert_raise test and just check the redirect, the test > passes. > > Is it because I already rescue in the controller that the assertion > doesn''t see the error anymore? > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
deegee
2008-Jul-25 11:33 UTC
Re: Nothing raised in functional test if exception is recued in controller?
Ganesh, Thanks for the explanation. I thought it might be related to me handling the exception in the controller. But if I don''t handle it in the controller, how do I handle it? In the example you give, the controller just throws the exception and it never gets handled by my application, right. My guess would be to make a special exception handler in the application controller? But which method do I need to override to make my own rescue handler? I''m only familiar with the begin/rescue construct and I must say the books and documentation I have used up to now are very succinct on exception handling. Always a weak point I find, where error handling is actually the most important thing! Dirk. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---