[mocha-developer] Expect with "real world" returns?

Zach Moazeni zach.lists at gmail.com
Wed May 21 16:54:36 EDT 2008

On May 21, 2008, at 4:35 PM, Carl Mercier wrote:

> I think you missed my point.  I -do- want the real database row, but  
> I also want to make sure that the "find" method is called with the  
> right parameters and x number of times.

I'll chalk it up to different testing strategies using mocking, but to  
my recollection, I've never come across the need for my code to  
execute real code as well as assert the parameters and number of times  
the function was called. But I'm speaking from my experience, and my  
needs which are most likely different from yours.

If I'm concerned with the parameters, I'll stub or expect it  
(depending on the situation), and return either an existing object or  
stub. But even when I'm testing ActiveRecord, I rarely send back an  
instance of an ActiveRecord object. If the code under test requires a  
real instance, and can't succinctly use a stub, it starts to smell as  
if the code is doing too much to that instance.

Another thing to note, is that I'm a big fan of returning literals  
when behavior is not required on the return value itself.

Car.stubs(:find).with(1).returns("the car")

Car.stubs(:find).with(:all, :conditions => ["color = ?",  
"red"]).returns(["car 1", "car 2"])

This is very useful, and readable, when the return becomes a parameter  
for another function, or the return is used in an algorithm (like  
reversing an array).


More information about the mocha-developer mailing list