[mocha-developer] When to stub/when to mock (was Rails functional testing and Mocha)

David Chelimsky dchelimsky at gmail.com
Mon Mar 5 11:21:43 EST 2007


On 3/5/07, James Mead <jamesmead44 at gmail.com> wrote:
> On 05/03/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> >
> > I have a different perspective on when to use stubs and when to use
> > mocks and I'm curious as to your thoughts about it.
> >
> > I like to put stubs in setup, regardless of whether they are commands
> > or queries, and mock expectations in test methods (specify blocks in
> > rspec). This keeps the noise of what is necessary but uninteresting
> > (from the perspective of the tests) out of the way of the test
> > methods, which can focus entirely on the interesting bits.
> >
> > The motivation is the same as Nat Pryce's - expecting only the
> > interesting bits will make tests less brittle - but the resulting
> > principle revolves on a different axis.
> >
> > Thoughts on this?
> >
>
> Hi David,
>
> I don't think we are thinking very differently.

I'm quite confident that our goals are the same - readable and robust
(non-brittle) tests.

> I think of the stubs as providing an environment for the object under test.
> Whether and how the object under test chooses to query this environment is
> an implementation detail. So I stub rather than expect. Does that make
> sense?
>
> I'm not sure what you mean by "revolving on a different axis".

Sorry - that was a vague metaphor. What I mean is that you're using
the "what" (query vs command) to decide whether to stub or expect,
whereas I use the "where" (setup vs test method).

I approach it this way for readability. I want to be able to look at a
test method and not have to look anywhere else to understand what it
is specifying. One weird side-effect is that I often end up stubbing
things in setup that I then "expect" explicitly in the test method.
That way the other tests using the same setup are guaranteed (well,
more likely ;) ) to run, but the tests that are actually interested in
a particular detail express that expectation explicitly.

Make sense?

>
> --
> James.
> http://blog.floehopper.org
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer
>


More information about the mocha-developer mailing list