[rspec-users] specing rescue, ensure and else blocks of an Exception

Tarsoly András tarsolya at gmail.com
Fri Oct 26 06:55:18 EDT 2007


Greetings,

I'm using rspec with rcov for my applications and there is one issue  
which I cannot solve nor can find any proper information regarding  
it: specing what is in a rescue block in case of an exception.

I'm using Ruby on Rails and I usually make use of exceptions in my  
controllers, like the following example:

def action
   @foo = Foo.find(1)
   @foo.update_attributes!(params[:foo])
   redirect_to :back
rescue ActiveRecord::RecordInvalid => e
   ...exception handling code ...
rescue Exception => e
   ...exception handling code ...
else
   ...else block
ensure
   ...ensure block
end

I have a specs for this, like this one:

it "should retrieve a Foo instance, identified by ID and update it's  
parameters" do
   Foo.should_receive(:find).with("1").and_return(@foo)
   @foo.should_receive(:update_attributes!).with 
(valid_params).and_return(@foo)
   get :action, :id => 1
   response.should be_redirect
end

it "should retrieve a Foo instance, identified by ID and throw an  
exception, because of bad parameters" do
   Foo.should_receive(:find).with("1").and_return(@foo)
   @foo.should_receive(:update_attributes!).with 
(valid_params).and_return(@foo)
   get :action, :id => 1
   response.should raise_error
end

or

it "should retrieve a Foo instance, identified by ID and throw an  
exception, because of bad parameters" do
   Foo.should_receive(:find).with("1").and_return(@foo)
   lambda { @foo.should_receive(:update_attributes!).with 
(valid_params).and_return(@foo) }.should raise_error
   get :action, :id => 1
end


... and ... that's it, the tests pass, becaue an Exception is raised,  
but I don't know how to proceed with testing all the rescue, ensure  
and else blocks. Since rcov reporting it as not being run, my  
coverage is suffering and I really don't want to release code which  
are only being partially tested.

So any pointers, help or links to some quick rundown regarding this  
problem would be greatly appreciated.

Thanks a bunch and my best regards,
András

--
Tarsoly András
tarsolya at gmail.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071026/bfb8b197/attachment-0001.html 


More information about the rspec-users mailing list