<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Greetings,<div><br class="webkit-block-placeholder"></div><div>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.</div><div><br class="webkit-block-placeholder"></div><div>I'm using Ruby on Rails and I usually make use of exceptions in my controllers, like the following example:</div><div><br class="webkit-block-placeholder"></div><div>def action</div><div>  @foo = Foo.find(1)</div><div>  @foo.update_attributes!(params[:foo])</div><div>  redirect_to :back</div><div>rescue ActiveRecord::RecordInvalid =&gt; e</div><div>  ...exception handling code ...</div><div>rescue Exception =&gt; e</div><div>  ...exception handling code ...</div><div>else</div><div>  ...else block</div><div>ensure</div><div>  ...ensure block</div><div>end</div><div><br class="webkit-block-placeholder"></div><div>I have a specs for this, like this one:</div><div><br class="webkit-block-placeholder"></div><div>it "should retrieve a Foo instance, identified by ID and update it's parameters" do</div><div>  Foo.should_receive(:find).with("1").and_return(@foo)</div><div>  @foo.should_receive(:update_attributes!).with(valid_params).and_return(@foo)</div><div>  get :action, :id =&gt; 1</div><div>  response.should be_redirect</div><div>end</div><div><br class="webkit-block-placeholder"></div><div>it "should retrieve a Foo instance, identified by ID and throw an exception, because of bad parameters" do</div><div>  Foo.should_receive(:find).with("1").and_return(@foo)</div><div>  @foo.should_receive(:update_attributes!).with(valid_params).and_return(@foo)</div><div>  get :action, :id =&gt; 1</div><div>  response.should raise_error</div><div>end</div><div><br class="webkit-block-placeholder"></div><div>or</div><div><br class="webkit-block-placeholder"></div><div><div>it "should retrieve a Foo instance, identified by ID and throw an exception, because of bad parameters" do</div><div>  Foo.should_receive(:find).with("1").and_return(@foo)</div><div>  lambda { @foo.should_receive(:update_attributes!).with(valid_params).and_return(@foo) }.should raise_error</div><div>  get :action, :id =&gt; 1</div><div>end</div><br class="webkit-block-placeholder"></div><div><br class="webkit-block-placeholder"></div><div>... 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.</div><div><br class="webkit-block-placeholder"></div><div>So any pointers, help or links to some quick rundown regarding this problem would be greatly appreciated.</div><div><br class="webkit-block-placeholder"></div><div>Thanks a bunch and my best regards,</div><div>András</div><div> </div><div>--</div><div><div><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0; "><div>Tarsoly András</div><div><a href="mailto:tarsolya@gmail.com">tarsolya@gmail.com</a></div><div><br class="webkit-block-placeholder"></div><br class="Apple-interchange-newline"></span> </div><br></div></body></html>