Rescue

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…

On Tue, Mar 18, 2008 at 11:13 PM, linojon [email protected] wrote:

flash[:notice] = "Unknown foo #{params[:id]}"

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,

yep, that did it :slight_smile:

thx

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs