[rspec-users] should raise_error

David Chelimsky dchelimsky at gmail.com
Thu Sep 25 13:35:43 EDT 2008

On Thu, Sep 25, 2008 at 12:33 PM, Nick Hoffman <nick at deadorange.com> wrote:
>> On Thu, Sep 25, 2008 at 9:33 AM, Nick Hoffman <nick at deadorange.com> wrote:
>> I noticed in the RSpec documentation that "object.should raise_error"
>> isn't enclosed in a begin..rescue..end :
>> http://rspec.info/rdoc/classes/Spec/Matchers.html#M000420
>> However, when I use #raise_error , I have to rescue it, otherwise an
>> actual exception is raised and the script bails.
>> This:
>> RentalMap.make_marker('asdf').should raise_error(RuntimeError, 'The 1st
>> argument (property) should be a Property')
> On 2008-09-25, at 12:56, Mark Wilden wrote:
>> You need to send :should to a Proc:
>>  lambda { RentalMap.make_marker('asdf') }.should raise_error(blah, blah)
>> I'll be every RSpec user has made this mistake at least once (in my case,
>> numerous times). I wonder if it would be possible for the matcher to call
>> this out?
>> ///ark
> Thanks for that tip, Mark. When you have a minute, would you mind explaining
> why #should needs to be sent to a Proc?

Precisely because of the problem you're experiencing. If you look at
the code for Spec::Matchers::RaiseError, you'll see that it accepts a
proc and calls it in the context of a begin/rescue structure - saving
you from having to do that in your example.

Make sense?

More information about the rspec-users mailing list