Hi, whats the correct way to spec a rescue? This will raise it but doesnt test my code''s response # controller def edit @foo = Foo.find(params[:id]) rescue flash[:notice] = "Unknown foo #{params[:id]}" redirect_to foos_path end # spec it "should flash error if not found" do Foo.should_receive(:find).and_raise get :edit, :id => ''1'' flash[:notice].should == "Unknown foo 1" end # run Exception in... -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20080318/dc23a3bc/attachment-0001.html
On Tue, Mar 18, 2008 at 11:13 PM, linojon <linojon at gmail.com> wrote:> Hi, whats the correct way to spec a rescue? This will raise it but doesnt > test my code''s response > > > > # controller > > def edit > @foo = Foo.find(params[:id]) > rescue > flash[:notice] = "Unknown foo #{params[:id]}" > redirect_to foos_path > end > > > # spec > > it "should flash error if not found" do > Foo.should_receive(:find).and_raise > get :edit, :id => ''1'' > flash[:notice].should == "Unknown foo 1" > end > > # run > > Exception in... > >I have done a very similar thing: Foo.should_receive(:find).and_raise ActiveRecord::ActiveRecordError and then change your rescue to handle ActiveRecord::ActiveRecordError. If you get to the point where you have a generic way you''d like to handle the exceptions in a controller, or across controllers look into using the controller class method rescue_from. I''m not 100% this is what you were asking, hopefully you were just looking for confirmation, -- Zach Dennis http://www.continuousthinking.com
yep, that did it :) thx On Mar 18, 2008, at 11:50 PM, Zach Dennis wrote:> On Tue, Mar 18, 2008 at 11:13 PM, linojon <linojon at gmail.com> wrote: >> Hi, whats the correct way to spec a rescue? This will raise it but >> doesnt >> test my code''s response >> >> >> >> # controller >> >> def edit >> @foo = Foo.find(params[:id]) >> rescue >> flash[:notice] = "Unknown foo #{params[:id]}" >> redirect_to foos_path >> end >> >> >> # spec >> >> it "should flash error if not found" do >> Foo.should_receive(:find).and_raise >> get :edit, :id => ''1'' >> flash[:notice].should == "Unknown foo 1" >> end >> >> # run >> >> Exception in... >> >> > > I have done a very similar thing: > > Foo.should_receive(:find).and_raise ActiveRecord::ActiveRecordError > > and then change your rescue to handle ActiveRecord::ActiveRecordError. > > If you get to the point where you have a generic way you''d like to > handle the exceptions in a controller, or across controllers look into > using the controller class method rescue_from. > > I''m not 100% this is what you were asking, hopefully you were just > looking for confirmation, > > -- > Zach Dennis > http://www.continuousthinking.com > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users