
525
Variation: Test Spy
We can use a more capable version of a Test Stub, the Test Spy (page 538), as
an observation point for the indirect outputs of the SUT. Like a Test Stub, a
Test Spy may need to provide values to the SUT in response to method calls.
The Test Spy, however, also captures the indirect outputs of the SUT as it is
exercised and saves them for later verifi cation by the test. Thus, in many ways,
the Test Spy is “just a” Test Stub with some recording capability. While a Test
Spy is used for the same fundamental purpose as a Mock Object (page 544),
the style of test we write using a Test Spy looks much more like a test written
with a Test Stub.
Variation: Mock Object
We can use a Mock Object as an observation point to verify the indirect outputs
of the SUT as it is exercised. Typically, the Mock Object also includes the func-
tionality of a Test Stub in that it must return values to the SUT if it hasn’t already
failed the tests but the emphasis
1
is on the verifi cation of the indirect outputs.
Therefore, a Mock Object is a lot more than just a Test Stub plus assertions: It
is used in a fundamentally different way.
Variation: Fake Object
We use a Fake Object (page 551) to replace the functionality of a real DOC
in a test for reasons other than verifi cation of indirect inputs and outputs of
the SUT. Typically, a Fake Object implements the same functionality as the
real DOC but in a much simpler way. While a Fake Object is typically built
specifi cally for testing, the test does not use it as either a control point or an
observation point.
The most common reason for using a Fake Object is that the real DOC
is not available yet, is too slow, or cannot be used in the test environment
because of deleterious side effects. The sidebar “Faster Tests Without Shared
Fixtures” (page 319) describes how we encapsulated all database access behind
a persistence layer interface and then replaced the database with in-memory
hash tables and made our tests run 50 times faster. Chapter 6, Test Automation
Strategy, and Chapter 11, Using Test Doubles, provide an overview of the vari-
ous techniques available for making our SUT easier to test.
1
My mother grew up in Hungary and has retained a part of her Hungarian accent—think
Zsa Zsa Gabor—all her life. She says, “It is important to put the emphasis on the right
syllable.”
Test
Double
Test Double