[rspec-users] When to mock

Mark Wilden mark at mwilden.com
Tue Aug 19 10:14:45 EDT 2008

On Tue, Aug 19, 2008 at 6:38 AM, Jonathan Kinney <jonkinney at gmail.com>wrote:

> I noticed that the examples on the rspec website for model code
> http://rspec.info/documentation/rails/writing/models.html have no mocks or
> stubs. However both the controller example and view example do have mocks
> and stubs implemented. I was having some problems getting mocks to work in
> my model (using mock_model and passing in a hash or stubs) but I'm wondering
> if I shouldn't be mocking or stubbing in my model at all?

In a model spec, clearly you don't want to mock any part of the model under
test, because then you're not testing it. However, anything else (including
other models) is fair game. Mocks are used specifically to avoid testing
something, usually because you (or someone else) is testing it elsewhere.
Mocks make sure that the object under test is behaving correctly in its
interactions with other code - but (in a unit spec, at least), you don't
want to redundantly or expensively also test that other code.

That's my take, at least

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080819/b3df60ca/attachment.html>

More information about the rspec-users mailing list