[rspec-users] How to test for exceptions

Blake Pettersson blake.pettersson at hotmail.com
Sun May 20 11:55:39 EDT 2007


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


_________________________________________________________________
Connect to the next generation of MSN Messenger 
http://imagine-msn.com/messenger/launch80/default.aspx?locale=en-us&source=wlmailtagline


More information about the rspec-users mailing list