[rspec-users] mocking errors

Rupert rupespad1 at mac.com
Sun Jun 24 10:28:24 EDT 2007

> What is the correct way to mock out the errors on a Rails model?
> I'm assuming i need to say
> @mock_thing = mock_model(Thing)
> @mock_thing_errors = mock("errors")
> @mock_thing_errors.should_receive(:full_messages).and_return("An  
> error")
> @mock_thing.should_receive(:errors).and_return(@mock_thing_errors)
> Just wanted to check the best practice on this kind of thing and how
> other people handle it.

Don't know if this is the best way, but I do this sort of thing....

thing = Thing.create(:a_required_attribute => nil)
thing.should have_at_least(1).errors_on(:a_required_attribute)

I don't think you need to mock the error handling code itself, you  
just want to check that the result of passing an invalid attribute to  
a model results in an error on that attribute.

Hope this makes sense



More information about the rspec-users mailing list