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