[rspec-users] Mock and Stub objects

Matt Wynne matt at mattwynne.net
Thu Jun 11 04:43:48 EDT 2009

On 11 Jun 2009, at 08:17, Amit Kulkarni wrote:

> David Chelimsky wrote:
>> On Wed, Jun 10, 2009 at 5:55 AM, Amit Kulkarni<lists at ruby-forum.com>
>> wrote:
>>> Hello,
>>> � � �I had seen many controller spec using mock and stub  
>>> objects.
>>> � � �I had written controller spec earlier without these  
>>> objects.
>>> � � �In the book it is mention about mock and stub  
>>> objects.but i am
>>> still not getting the reason behind it.
>> Did you read the chapter on Mock Objects?
> Yes i have read Mock Objects.
> I understood a bit that Mock object create objects virtually so that  
> we
> can run our specs.It helps us in that we dont have to actually create
> those objects.
> But i am still not getting my hand on it.
> we may have a situation that just for instance that all the  
> development
> is done and we are now writing the spec for it.In this case all the
> coding in controller,model are done.
> So when we write specs for controller do we have to create mock  
> objects?
> What if we write specs in controller without using mock objects?Will  
> it
> be fine?

If you're writing the specs after you've written the code, you're  
losing a lot of the value of mock objects.

Mock objects were originally devised as a design tool for use when  
doing TDD. TDD stands for Test *Driven* Development, meaning that you  
write the test before you have written any of the code. When you're  
doing this, you want to be able to build the system piece by piece,  
class by class, so you write tests for a single object at a time, and  
use mock objects to assemble quick, lightweight sketches of the  
surrounding objects that you imagine it will interact with when the  
system is finished.

> Also since i am using the beta version Mocking Objects part is not
> there.And it is does not give clearcut idea from the topic Mock  
> Objects.
> Also it will be very helpful if you can provide me a link or anything
> which can give me details of Mocks and Stubs.How to use it?When to use
> it..e.t.c

This is a big topic which is not going to be easy to explain over  
email. Here is some more stuff for you to read.


Matt Wynne

More information about the rspec-users mailing list