Should raise_error

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’)

Results in this:
1)
RuntimeError in ‘RentalMap#make_marker should raise an exception for a
non-Property argument’
The 1st argument (property) should be a Property
/Users/nick/src/myapp.podoboo.com/app/models/rental_map.rb:127:in
`make_marker’
./spec/models/rental_map_spec.rb:218:
script/spec:4:

Whereas this succeeds:
begin
RentalMap.make_marker(‘asdf’).should raise_error(RuntimeError, ‘The
1st argument (property) should be a Property’)
rescue; end

Is this how #raise_error is supposed to work, or am I doing something
wrong?

Thanks,
Nick

On Thu, Sep 25, 2008 at 9:33 AM, Nick H. [email protected]
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

On Thu, Sep 25, 2008 at 11:56 AM, Mark W. [email protected] wrote:

RentalMap.make_marker(‘asdf’).should raise_error(RuntimeError, ‘The 1st
argument (property) should be a Property’)

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?

Anything is possible :slight_smile:

Please submit a feature request: http://rspec.lighthouseapp.com/

Cheers,
David

On Thu, Sep 25, 2008 at 12:33 PM, Nick H. [email protected]
wrote:

argument (property) should be a Property’)

///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?

On 2008-09-25, at 13:35, David C. wrote:

Make sense?

I just had a peek at Spec::Matchers::RaiseError#matches? and see the
begin…rescue there. Thanks, David.

RentalMap.make_marker(‘asdf’).should raise_error(RuntimeError, ‘The
1st argument (property) should be a Property’)

On 2008-09-25, at 12:56, Mark W. 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?