I''m trying to spec a dead simple "show non-existent record should render 404" case, but it seems the RecordNotFound exception is making it impossible for some reason. #controller def show @event = Event.find(params[:id]) end #spec - pretty much straight from the rspec site before do Event.stub!(:find) get :show, :id => ''broken'' end # passes it "should not assign an event" do assigns[:event].should == nil end # fails - expected 404 but got events/show it "should render 404" do response.should render_template("#{RAILS_ROOT}/public/404.html") response.response_code.should == 404 end #end spec Because I''m stubbing find does that mean the RecordNotFound exception doesn''t happen and thus the 404 doesn''t render? I tried stubbing and using should_raise, and_raise, whatever the heck it is, but I just don''t get it, nothing seems to work. It behaves as expected when browser tested! :O I''m going to spontaneously combust! [RSpec 1.1.2, Rails 2.0.2, ZenTest 3.8.0] -- Posted via http://www.ruby-forum.com/.
Try Event.stub!(:find).and_raise(RecordNotFound) On 2/13/08, Brad Carson <lists at ruby-forum.com> wrote:> I''m trying to spec a dead simple "show non-existent record should render > 404" case, but it seems the RecordNotFound exception is making it > impossible for some reason. > > #controller > def show > @event = Event.find(params[:id]) > end > > #spec - pretty much straight from the rspec site > before do > Event.stub!(:find) > get :show, :id => ''broken'' > end > > # passes > it "should not assign an event" do > assigns[:event].should == nil > end > > # fails - expected 404 but got events/show > it "should render 404" do > response.should render_template("#{RAILS_ROOT}/public/404.html") > response.response_code.should == 404 > end > #end spec > > Because I''m stubbing find does that mean the RecordNotFound exception > doesn''t happen and thus the 404 doesn''t render? I tried stubbing and > using should_raise, and_raise, whatever the heck it is, but I just don''t > get it, nothing seems to work. It behaves as expected when browser > tested! :O > > I''m going to spontaneously combust! > > [RSpec 1.1.2, Rails 2.0.2, ZenTest 3.8.0] > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Pat Maddox wrote:> Try > > Event.stub!(:find).and_raise(RecordNotFound)To my credit, I did try that (though ActiveRecord::RecordNotFound, of course) but then I get: EventsController attempt to show non-existing event - should render 404 (ERROR - 2) - should not assign an event (ERROR - 3) ActiveRecord::RecordNotFound in ''EventsController attempt to show non-existing event should render 404'' ActiveRecord::RecordNotFound Mind-boggling. -- Posted via http://www.ruby-forum.com/.
On 13.2.2008, at 21.09, Brad Carson <lists at ruby-forum.com> wrote:> Pat Maddox wrote: >> Try >> >> Event.stub!(:find).and_raise(RecordNotFound) > > To my credit, I did try that (though ActiveRecord::RecordNotFound, of > course) but then I get: > > EventsController attempt to show non-existing event > - should render 404 (ERROR - 2) > - should not assign an event (ERROR - 3) > > ActiveRecord::RecordNotFound in ''EventsController attempt to show > non-existing event should render 404'' > ActiveRecord::RecordNotFound > > Mind-boggling.The exception is never rescued. You''d have to catch the exception yourself, I think, and render the 404 page. I might be talking through my ass but I think the built in rescue of AR::RNF only works in production. //jarkko> > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Jarkko Laine wrote:> On 13.2.2008, at 21.09, Brad Carson <lists at ruby-forum.com> wrote: > >> - should not assign an event (ERROR - 3) >> >> ActiveRecord::RecordNotFound in ''EventsController attempt to show >> non-existing event should render 404'' >> ActiveRecord::RecordNotFound >> >> Mind-boggling. > > The exception is never rescued. You''d have to catch the exception > yourself, I think, and render the 404 page. I might be talking through > my ass but I think the built in rescue of AR::RNF only works in > production. > > //jarkkoI was led to believe Rails automatically handled RecordNotFounds with a 404, meaning I didn''t have to catch them myself. I made sure requests weren''t considered local in the test environment to trigger the 404 page instead of the debug info. I tried putting a rescue_action_in_public in the application controller to handle the RecordNotFound and do the 404 render, just in case, but the spec won''t pass unless I rescue the exception inside the show method, which doesn''t seem right or necessary. -- Posted via http://www.ruby-forum.com/.
> I was led to believe Rails automatically handled RecordNotFounds with a > 404, meaning I didn''t have to catch them myself. > > I made sure requests weren''t considered local in the test environment to > trigger the 404 page instead of the debug info. I tried putting a > rescue_action_in_public in the application controller to handle the > RecordNotFound and do the 404 render, just in case, but the spec won''t > pass unless I rescue the exception inside the show method, which doesn''t > seem right or necessary.Aha! Had to use: controller.use_rails_error_handling! Here''s the complete spec: http://pastie.caboo.se/151743 As per the pasite, requests have to be non-local. Thank you to everyone who pitched in to help! -- Posted via http://www.ruby-forum.com/.