# Mocha stub and assert\_raises

**URL:** <https://www.ruby-forum.com/t/mocha-stub-and-assert-raises/180064>\
**Category:** Rails\
**Created:** [December 10, 2009, 3:33pm UTC](https://www.ruby-forum.com/t/mocha-stub-and-assert-raises/180064 "2009-12-10T15:33:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [December 10, 2009, 3:33pm UTC](https://www.ruby-forum.com/t/mocha-stub-and-assert-raises/180064/1 "2009-12-10T15:33:52Z")

</div>

Hello all,

Im trying to test that my controller can handle a RuntimeError exception  
which may be thrown by one of my model methods. Ive stubbed, with mocha,  
the model method to raise such an error but my assert\_raise keeps  
complaining that nothing gets raised.

Ive checked my test.log and I know that the assert is being raised (the  
one defined in my stub) and its being rescued because i added a message  
to the exception being raised by my stub in the test, and its visible in  
the log. I also know that its being rescued because i placed a logger  
call inside the resuce block and its also showing in the test.log So why  
is the test failing??

my code is here

test “destroy should catch exception raised by spreedly during  
cancellation” do  
login\_larry\_active  
Subscription.any\_instance.stubs(:stop\_auto\_renew).raises(RuntimeError,  
“if this isnt being thrown then this shouldnt show up in my test log”)  
assert\_raises do  
delete :destroy, {:user\_id =\> @logged\_in\_user.id}  
end  
assert flash[:notice]  
assert\_redirected\_to :controller =\> :dashboard, :action =\> :welcome  
end

Controller

#note that ldb() is just a method which writes to test.log  
def destroy  
user = User.find(params[:user\_id])  
if user  
begin  
user.subscription.stop\_auto\_renew  
rescue RuntimeError =\> e  
ldb(“cant process stop\_auto\_renew for user id: #{user.id}”)  
ldb(“exception : #{e}”)  
end  
flash[:notice] = “You have successfully cancelled your  
subscription and will take effect at the end of your current billing  
month”  
end  
redirect\_to :controller =\> :dashboard, :action =\> :welcome  
end

Test.log

## Starting test\_destroy\_should\_catch\_exception\_raised\_by\_spreedly\_during\_cancellation

e[4;35;1mUser Load (0.1ms)e[0m e[0mSELECT \* FROM `users` WHERE  
(`users`.`id` = 2) e[0m  
e[4;36;1mSubscription Load (0.0ms)e[0m e[0;1mSELECT \* FROM  
`subscriptions` WHERE (`subscriptions`.user\_id = 2) LIMIT 1e[0m

Processing SubscriptionsController#destroy (for 0.0.0.0 at 2009-12-10  
20:18:46) [DELETE]  
Parameters: {“user\_id”=\>“2”}  
e[4;35;1mUser Load (0.1ms)e[0m e[0mSELECT \* FROM `users` WHERE  
(`users`.`id` = 2) e[0m  
e[4;36;1mSubscription Load (0.0ms)e[0m e[0;1mSELECT \* FROM  
`subscriptions` WHERE (`subscriptions`.user\_id = 2) LIMIT 1e[0m

### 

/home/adam/railsprojects/spreedly/app/controllers/subscriptions\_controller.rb:26:in  
`destroy’: Spreedly cant process stop\_auto\_renew for user id: 2

### 

/home/adam/railsprojects/spreedly/app/controllers/subscriptions\_controller.rb:27:in  
`destroy’: exception : if this isnt being thrown then this shouldnt show  
up in my test log  
Redirected to [http://test.host/dashboard/welcome](http://test.host/dashboard/welcome)  
Completed in 2ms (DB: 0) | 302 Found  
[[http://test.host/users/2/subscription](http://test.host/users/2/subscription)]  
e[4;35;1mSQL (0.1ms)e[0m e[0mROLLBACKe[0m  
e[4;36;1mSQL (0.0ms)e[0m e[0;1mBEGINe[0m
