Forum: RSpec [RSpec] #and_raise

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nick H. (Guest)
on 2009-02-16 22:41
(Received via mailing list)
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
David C. (Guest)
on 2009-02-17 00:20
(Received via mailing list)
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
Nick H. (Guest)
on 2009-02-18 17:46
(Received via mailing list)
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!
This topic is locked and can not be replied to.