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
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> -Nick > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
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, > DavidThat''s great. Thanks, David!