[rspec-users] How to test for exceptions

Dylan Egan dylanegan at gmail.com
Sun May 20 16:53:11 EDT 2007


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


More information about the rspec-users mailing list