Search the web
Sign In
New User? Sign Up
testdrivendevelopment · Test-driven Development
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want your group to be featured on the Yahoo! Groups website? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Spy vs. Mock   Message List  
Reply | Forward Message #5298 of 32000 |
Everyone:

I want to focus on this part of the recent conversation about the
taxonomy of alternate implementations of interfaces. I would like to
discuss whether there is a difference between a Spy and Mock. In
particular, I'd like to know whether I'm just over-defining things.

The Spy and the Mock solve the same problem: each stands in for a given
interface and records information about how it was used, allowing the
test to verify that use.

A Spy is an alternate implementation that records useful information
(which the programmer decides on a case-by-case basis) about how some
object used it. The test then verifies the information the Spy gathered.
I chose the term "Spy" because it feels like the object is gathering
intelligence for future debriefing.

A Spy event listener might just collect the events it received from
various event sources, then provide a method for the test to retrieve
the events.

A Mock Object (http://www.mockobjects.com) is an alternate
implementation that records your expectations about specifically which
methods will be invoked, with which parameters, returning which values
and in which sequence. In "replay" mode, objects use the Mock Object.
The test then asks the Mock to verify that its actual method invocation
sequence (and all the accompanying details) match the test's expectation.

A Mock event listener would not collect the events it received, but
rather have a log of each method invocation, with all the details, and
would verify that against expectations we set when we "recorded" the Mock.

I see the following difference.

A Spy gathers whatever information the programmer thinks is appropriate.
A Mock gathers specific information about method invocation sequences.

We can make a Spy act like a Mock with its record, replay and verify
modes, so I do not consider that to be an essential difference.

The way I see it, a Mock is like an off-the-shelf Spy, but a Spy is not
necessarily a Mock.

Is there value in having different terms? I assume that over time we
will begin to confuse Mock Objects (http://www.mockobjects.com) with
mock objects (which is a concept that appears to be the same as what I
call a Spy). I assume that when we write mock object we will mean Mock
Object, a particular implementation of mock objects. In that case, are
we wise to make a better distinction between the two? Will it make
communication clearer among mock objects users?
--
J. B. Rainsberger,
Diaspar Software Services
http://www.diasparsoftware.com :: +1 416 791-8603
Let's write software that people understand






Fri Oct 3, 2003 4:38 pm

nails762
Offline Offline
Send Email Send Email

Forward
Message #5298 of 32000 |
Expand Messages Author Sort by Date

Everyone: I want to focus on this part of the recent conversation about the taxonomy of alternate implementations of interfaces. I would like to discuss...
J. B. Rainsberger
nails762
Offline Send Email
Oct 3, 2003
6:22 pm

... It may be that I'm just out of touch with the practices and terminology of the TDD community, but this "recording and verifying method invocation...
Colin Putney
colinputney
Offline Send Email
Oct 3, 2003
7:18 pm

... I agree. It is possible that a method invocation sequence is the expectation, but not required. But it is the immediate declaration of a failure that is...
Russell Gold
russgold
Online Now Send Email
Oct 3, 2003
10:33 pm

... Hmm, good point. I hadn't considered self-shunt as an example of a Spy, but that's exactly what it is, and I use it quite often. I guess there is something...
Colin Putney
colinputney
Offline Send Email
Oct 4, 2003
10:34 pm

... I'm beginning to see this as a key property of mocks, too. Ideas are reforming.... ... As someone writing a book on testing, describing approaches to...
J. B. Rainsberger
nails762
Offline Send Email
Oct 6, 2003
10:32 pm

... In TDD, you make the test compile on the way to testing the object in isolation from its collaborators. That is, you *always* turn a "stub" into a "fake."...
Russell Gold
russgold
Online Now Send Email
Oct 8, 2003
3:51 pm

... Yes: the cases where I go from a stub directly to Obvious Implementation, bypassing Triangulation. A fake never exists. ... Simply put: I intentionally and...
J. B. Rainsberger
nails762
Offline Send Email
Oct 8, 2003
4:33 pm

... Ah, I see what you mean. I had considered that to be "stubbing out" methods in the actual class, not "writing a stub." I consider a stub class to be a...
Russell Gold
russgold
Online Now Send Email
Oct 9, 2003
3:10 am

... Well, a stub's a stub, right? :) So you're talking about stubbing out a collaborator and not having to worry, for this test, about how that particular...
J. B. Rainsberger
nails762
Offline Send Email
Oct 9, 2003
5:55 pm

... Why?...
steve@...
smg_freeman
Online Now Send Email
Oct 8, 2003
4:20 pm

... Mostly because most of the places where I had previously thought mocks to be important in TDD turn out to be addressed very easily without them. I still...
Russell Gold
russgold
Online Now Send Email
Oct 8, 2003
4:33 pm

... This is exactly what I have been finding over the past 18 months. ... I use them more as a last resort. I don't reach for them as quickly as I used to. I'm...
J. B. Rainsberger
nails762
Offline Send Email
Oct 9, 2003
3:10 am

... That's funny, because we're moving in the opposite direction. We're starting to think that mocking up third part libraries was a bit of a distraction from...
Steve Freeman
smg_freeman
Online Now Send Email
Oct 9, 2003
3:10 am

One question: Does a Spy ever return a value? You talk about event listeners, but what about other situations? You mentioned JDBC as an example--what would you...
Joel Shellman
joelshellman
Offline Send Email
Oct 3, 2003
10:33 pm

... A Spy may collect method return values, if that was important. A Spy doesn't return a value, though, as that's not defined. A Spy is a class, not a method....
J. B. Rainsberger
nails762
Offline Send Email
Oct 6, 2003
10:32 pm

... as noted in another message, this misses the point. Our use of mocks for record and playback is only as a last resource when we can't do the right thing,...
Steve Freeman
smg_freeman
Online Now Send Email
Oct 3, 2003
11:37 pm

... Again, this is sloppy on my part and due to my use of EasyMock. I need to explore writing custom MockObjects in more detail, perhaps. ... I need better...
J. B. Rainsberger
nails762
Offline Send Email
Oct 6, 2003
10:32 pm

... I think that decision of whether or not to verify method invocation sequences should be configurable in any good "mock object" framework. I don't think it...
Chad Woolley
thewoolleyman
Offline Send Email
Oct 8, 2003
3:51 pm

... Whether they /should/ is not at issue. Whether they do, /de facto/, is the issue. I am predicting that, over time, they will, at least until some other...
J. B. Rainsberger
nails762
Offline Send Email
Oct 8, 2003
4:33 pm

... Here's a thought: What if my framework allows me to configure these features on a per-method-invocation basis, for a single "object"? For example, my...
Chad Woolley
thewoolleyman
Offline Send Email
Oct 9, 2003
3:10 am

... It's a Mock. As soon as it shows a sign of being a Mock, it's a Mock. That's just my opinion. ... I can see where this would be useful. ... No. I use the...
J. B. Rainsberger
nails762
Offline Send Email
Oct 9, 2003
5:55 pm

... I think that most of the mock object frameworks should be usable in the way you describe (although dynamic/virtual approaches do lack reusability). What do...
Chad Woolley
thewoolleyman
Offline Send Email
Oct 9, 2003
1:30 pm

... I'm not sure that many people are /misusing/ them. Just some seem to be missing the point. Largely because we haven't done a proper job of education. ... ...
Steve Freeman
smg_freeman
Online Now Send Email
Oct 13, 2003
3:20 pm

... I don't think Java forces you to do this at all (I'm not a C programmer, so I won't speak for that). I maintain that the ability to define mock/fake/stub...
Chad Woolley
thewoolleyman
Offline Send Email
Oct 9, 2003
7:35 pm

... Sure it does: you have to create a new type, often by subclassing. In Ruby, you just substitute another object that understands the same messages and...
J. B. Rainsberger
nails762
Offline Send Email
Oct 10, 2003
5:36 pm

... I'm not getting you here. If your test requires you to mock some method of collaborator class A, how does moving that method to collaborator class B...
Chad Woolley
thewoolleyman
Offline Send Email
Oct 10, 2003
5:36 pm

... It doesn't. But if I mock object A and notice that I am reimplementing some of its methods, /that/ is the signal to extract object B from A. ... If C uses...
J. B. Rainsberger
nails762
Offline Send Email
Oct 10, 2003
6:27 pm

... that would be a smell, because we never, never duplicate production code in a mock. ... True, but maybe you need a mock for B then. Depends on how big it...
Steve Freeman
smg_freeman
Online Now Send Email
Oct 13, 2003
3:20 pm

... ...and you're never, never tempted to do it? You never, never arrive at a situation where you think, "Damn, if I kept going this way, I'll end up...
J. B. Rainsberger
nails762
Offline Send Email
Oct 14, 2003
7:31 pm
First  | < Prev  |  Next > Last 
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help