[rspec-users] When to mock

Matt Wynne matt at mattwynne.net
Tue Aug 19 10:21:14 EDT 2008

It's also, for me, nice to isolate the code I'm testing using mocks.  
So if I'm building a controller and I mock out the behaviour I will  
expect it to call on the model layer, I know that any failing tests  
must be due to bugs in the controller class, nowhere else.

Coming from using an NHibernate / POCO stack in .NET where my domain  
(model) objects were completely de-coupled from their persistence  
behaviour, I've found it quite hard working with ActiveRecord  
objects. It seems like the path of least resistance when you're  
working with objects that inherit from ActiveRecord::Base is just to  
put your TDD principles to one side, set up stock data in your  
database, call the model from your tests and forget about trying to  
mock anything out. Trying to get a mockable seam in between your own  
model logic and ActiveRecord's persistence code seems to hard... at  
least for me at this stage of my ruby learnings.

So, in summary, I guess I would advise you to mock wherever you can,  
unless you're testing an object that inherits from ActiveRecord::Base.

People draw the line in lots of different places though, so you'll  
probably find other people much less keen on mocking than me.


In case you wondered: The opinions expressed in this email are my own  
and do not necessarily reflect the views of any former, current or  
future employers of mine.

On 19 Aug 2008, at 15:08, Scott Taylor wrote:

> On Aug 19, 2008, at 9:38 AM, Jonathan Kinney 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? I've done the googling on  
>> mocks vs stubs and read a bunch or info, I'm just wondring how  
>> some of the fellow rspec users implement mocking and stubbing in  
>> their specs and whether or not it is ok to mock and or stub in a  
>> model?
> Of course it's OK to mock - but with that said, you should probably  
> have some sort of "real" integration tests, which do touch the  
> database.
> Ultimately, it's all about convenience and your comfort level.  The  
> reason mocking is as popular as it is is simply because of slow  
> test suites (google Dan Manges and unitrecord, if you are curious).
> Scott
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

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

More information about the rspec-users mailing list