[rspec-users] [RSpec] #and_raise

Nick Hoffman nick at deadorange.com
Wed Feb 18 10:36:22 EST 2009


On 16/02/2009, at 4:12 PM, David Chelimsky wrote:
> On Mon, Feb 16, 2009 at 1:48 PM, Nick Hoffman <nick at deadorange.com>  
> wrote:
>> 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?
>
> 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

That's great. Thanks, David!


More information about the rspec-users mailing list