[rspec-users] How to test for exceptions

Blake Pettersson blake.pettersson at hotmail.com
Mon May 21 05:54:10 EDT 2007


This was just what I needed, thanks!



----------------------------------------
> Date: Sun, 20 May 2007 13:53:11 -0700
> From: dylanegan at gmail.com
> To: rspec-users at rubyforge.org
> Subject: Re: [rspec-users] How to test for exceptions
> 
> You want update_attributes! to raise the exception. Otherwise if
> you're returning false you'll want to be using update_attributes and
> if else statements.
> 
> So something like
> 
> it "should ..." do
>   @venue.should_receive(:update_attributes!).with("name" => "random
> text").and_raise(ActiveRecord::RecordInvalid.new(mock_invalid_record))
>   do_update_call
> end
> 
> it "should ..." do
>   do_update_call
>   response.should render_template(:edit)
> end
> 
> This is the mock I create for invalid records.
> 
>  def mock_invalid_record(name = 'invalid_record')
>     invalid_record, errors = mock(name), mock('errors')
>     invalid_record.stub!(:errors).and_return(errors)
>     errors.stub!(:full_messages).and_return(['errors'])
>     invalid_record
>   end
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

_________________________________________________________________
Invite your mail contacts to join your friends list with Windows Live Spaces. It's easy!
http://spaces.live.com/spacesapi.aspx?wx_action=create&wx_url=/friends.aspx&mkt=en-us


More information about the rspec-users mailing list