# Rescue

**URL:** <https://www.ruby-forum.com/t/rescue/132316>\
**Category:** RSpec\
**Created:** [March 19, 2008, 4:39am UTC](https://www.ruby-forum.com/t/rescue/132316 "2008-03-19T04:39:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![linojon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b3f665/32.png) [@linojon](https://www.ruby-forum.com/u/linojon)\
**Post date:** [March 19, 2008, 4:39am UTC](https://www.ruby-forum.com/t/rescue/132316/1 "2008-03-19T04:39:47Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![linojon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b3f665/32.png) [@linojon](https://www.ruby-forum.com/u/linojon)\
**Post date:** [March 19, 2008, 4:51am UTC](https://www.ruby-forum.com/t/rescue/132316/2 "2008-03-19T04:51:30Z")

</div>

On Tue, Mar 18, 2008 at 11:13 PM, linojon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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,

---

<div class="post-metadata">

**Author:** ![linojon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b3f665/32.png) [@linojon](https://www.ruby-forum.com/u/linojon)\
**Post date:** [March 19, 2008, 7:01am UTC](https://www.ruby-forum.com/t/rescue/132316/3 "2008-03-19T07:01:03Z")

</div>

yep, that did it 🙂

thx
