[rspec-users] How to test for exceptions

aslak hellesoy aslak.hellesoy at gmail.com
Sun May 20 13:09:35 EDT 2007


On 5/20/07, Blake Pettersson <blake.pettersson at hotmail.com> wrote:
>
> Sorry about that, I should've been more clear. After sending the first message, I realised that I had sent a post to the wrong method in my spec (I sent it to create instead of update, as I meant to). Since I altered the code to reflect my true intention, so did the error message. Here's the new (and hopefully correct) spec + error message:
>
> describe VenuesController, "on update" do
>
>   before(:each) do
>     @venue = mock("venue")
>     @venue.stub!(:new_record?).and_return(false)
>     @venue.stub!(:to_param).and_return('1')
>     controller.class.send(:define_method, :rescue_action) { |e| raise e }
>   end
>
>  it "should raise an error if record is invalid" do
>     stub_authorized_login
>     Venue.should_receive(:find).and_return(@venue)
>     @venue.should_receive(:update_attributes!).with("name" => "random text").and_return(false)
>     lambda {put :update, :id => @venue, :venue => {"name" => "random text"}}.should raise_error(ActiveRecord::RecordInvalid)
>   end
> end
>
> 2)
> 'VenuesController on update should raise an error if record is invalid' FAILED
> expected ActiveRecord::RecordInvalid but nothing was raised
> ./spec/controllers/venues_controller_spec.rb:32:
> script/spec:4:
>

You generally don't want the controller to raise an error, as it will
utterly confuse your users to see a Ruby backtrace.
(Scaffold-generated controllers never raise exceptions). Instead you'd
want to render a special page when the update fails. Here is what I
would do:

describe VenuesController, "on update" do
  before(:each) do
    @venue = mock_model(Venue)
    Venue.should_receive(:find).and_return(@venue)
  end

  it "should re-render edit after unsuccessful update" do
    stub_authorized_login
    @venue.should_receive(:update_attributes!).and_return(false)
    put :update
    response.should render_template('edit')
  end
end

Aslak

> Line 32 is in this case the last line in the spec (i.e the error assertion).
>
> Blake
>
>
>
> ----------------------------------------
> > Date: Sun, 20 May 2007 09:40:08 -0700
> > From: aslak.hellesoy at gmail.com
> > To: rspec-users at rubyforge.org
> > Subject: Re: [rspec-users] How to test for exceptions
> >
> > Now I got confused. The error messages in your first post are not the
> > same as in the second one:
> >
> > 'VenuesController on update should raise an error if record is invalid' FAILED
> > expected ActiveRecord::RecordInvalid, got #
> >
> > versus
> >
> > 'VenuesController on update should raise an error if record is invalid' FAILED
> > expected ActiveRecord::RecordInvalid but nothing was raised
> > ./spec/controllers/venues_controller_spec.rb:32:
> > script/spec:4:
> >
> > Plese post a whole new email with both code and error messages.
> >
> > Aslak
> >
>
> > > The new error:
> > >
> > > 2)
> > > 'VenuesController on update should raise an error if record is invalid' FAILED
> > > expected ActiveRecord::RecordInvalid but nothing was raised
> > > ./spec/controllers/venues_controller_spec.rb:32:
> > > script/spec:4:
> > >
> > > line 32 is lambda {put :update, :venue => {"name" => "random text"}}.should raise_error(ActiveRecord::RecordInvalid)
> > >
> > > Regards,
> > > Blake
> > >
> > > _________________________________________________________________
> > > 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
> > > _______________________________________________
> > > rspec-users mailing list
> > > rspec-users at rubyforge.org
> > > http://rubyforge.org/mailman/listinfo/rspec-users
> > >
> > _______________________________________________
> > rspec-users mailing list
> > rspec-users at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-users
>
> _________________________________________________________________
> Explore the seven wonders of the world
> http://search.msn.com/results.aspx?q=7+wonders+world&mkt=en-US&form=QBRE
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list