David Chelimsky
2007-Mar-05 13:04 UTC
[mocha-developer] When to stub/when to mock (was Rails functional testing and Mocha)
Hi James,> From: James Mead <jamesmead44 at gmail.com> > Date: Mar 5, 2007 5:15 AM > Subject: Re: [mocha-developer] Rails functional testing and Mocha > To: mocha-developer at rubyforge.org> "I''d probably stub the call to find not expect it, because its a query not a command"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? Cheers, David
James Mead
2007-Mar-05 15:52 UTC
[mocha-developer] When to stub/when to mock (was Rails functional testing and Mocha)
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 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". -- James. http://blog.floehopper.org
David Chelimsky
2007-Mar-05 16:21 UTC
[mocha-developer] When to stub/when to mock (was Rails functional testing and Mocha)
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 >
James Mead
2007-Mar-05 16:51 UTC
[mocha-developer] When to stub/when to mock (was Rails functional testing and Mocha)
Yes - I understand what you are doing and agree that our goals are the same. Query vs Command isn''t the only way I decide whether to stub or expect. I (nearly) always stub Queries, but I don''t always expect Commands. Using the "one assertion per test" idea I select at most one Command to expect per test. Also I don''t tend to use a setup method. -- James. http://blog.floehopper.org
David Chelimsky
2007-Mar-05 17:03 UTC
[mocha-developer] When to stub/when to mock (was Rails functional testing and Mocha)
On 3/5/07, James Mead <jamesmead44 at gmail.com> wrote:> Yes - I understand what you are doing and agree that our goals are the same. > > Query vs Command isn''t the only way I decide whether to stub or expect. I > (nearly) always stub Queries, but I don''t always expect Commands. Using the > "one assertion per test" idea I select at most one Command to expect per > test. Also I don''t tend to use a setup method.Cool. Thanks for the feedback. Cheers, David> > -- > James. > http://blog.floehopper.org > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer >