[rspec-users] How to test for exceptions

Blake Pettersson blake.pettersson at hotmail.com
Sun May 20 12:27:16 EDT 2007


----------------------------------------
> Date: Sun, 20 May 2007 09:04:59 -0700
> From: aslak.hellesoy at gmail.com
> To: rspec-users at rubyforge.org
> Subject: Re: [rspec-users] How to test for exceptions
> 
> On 5/20/07, Blake Pettersson  wrote:
> >
> > Hi folks,
> >
> > I'm in the process of converting a Test::Unit functional test to RSpec, but I've run into a slight problem. I tried looking through the documentation but I still don't know what I missed.
> >
> > Thanks in advance,
> > Blake
> >
> > 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 {post :create, {:venue => @venue}}.should raise_error(ActiveRecord::RecordInvalid)
> >   end
> > end
> >
> > describe VenuesController, "on create" do
> >
> >   before(:each) do
> >     @venue = mock("venue")
> >     @venue.stub!(:new_record?).and_return(true)
> >     @venue.stub!(:to_param).and_return('1')
> >     Venue.stub!(:new).and_return(@venue)
> >     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(:save!).and_return(false)
> >     lambda {post :create, {:venue => @venue}}.should raise_error(ActiveRecord::RecordInvalid)
> >   end
> > end
> >
> > 1)
> > 'VenuesController on update should raise an error if record is invalid' FAILED
> > expected ActiveRecord::RecordInvalid, got #
> >
> > 2)
> > 'VenuesController on create should raise an error if record is invalid' FAILED
> > expected ActiveRecord::RecordInvalid but nothing was raised
> >
> 
> Is this shell output? Is there no backtrace?
> 
> Aslak
> 
> >
> > _________________________________________________________________
> > Connect to the next generation of MSN Messenger
> > http://imagine-msn.com/messenger/launch80/default.aspx?locale=en-us&source=wlmailtagline
> > _______________________________________________
> > 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

Yes it is shell output.

The full output follows:

3)
'VenuesController on create should raise an error if record is invalid' FAILED
expected ActiveRecord::RecordInvalid but nothing was raised
./spec/controllers/venues_controller_spec.rb:60:
script/spec:4:

line 60: lambda {post :create, {:venue => @venue}}.should raise_error(ActiveRecord::RecordInvalid)

I just realised that I posted to the wrong action on the update spec. The spec still doesn't pass though.

The modified code:

  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, :venue => {"name" => "random text"}}.should raise_error(ActiveRecord::RecordInvalid)
  end

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


More information about the rspec-users mailing list