[RSpec] #and_raise


#1

I often use #and_raise like so:

@error_message = ‘Some error’
@sf.should_receive(:shift_time!).and_raise @error_message

However, after trying to do this:

@argument_error = mock_model ArgumentError,
:message => @error_message
@sf.should_receive(:shift_time!).and_raise @argument_error

and then reading the docs for #and_raise, I realised that #and_raise
only accepts a String or exception class.

Is there a way to set the exception class and error message?
-Nick


#2

On Mon, Feb 16, 2009 at 1:48 PM, Nick H. removed_email_address@domain.invalid
wrote:

and then reading the docs for #and_raise, I realised that #and_raise only
accepts a String or exception class.

Is there a way to set the exception class and error message?

Actually, it accepts an exception class, exception object, or,
apparently (though not documented as such) just a string. Instead of a
mock, try a real ArgumentError:

@sf.should_receive(:shift_time).and_raise(ArgumentError.new(@error_message))

HTH,
David


#3

On 16/02/2009, at 4:12 PM, David C. wrote:

:message => @error_message
mock, try a real ArgumentError:

@sf
.should_receive
(:shift_time).and_raise(ArgumentError.new(@error_message))

HTH,
David

That’s great. Thanks, David!