
470
Chapter 21 Result Verification Patterns
SUT uses to interact with the components surrounding it make such observation
possible; when this is not the case, we must install some sort of Test Double to
monitor the SUT’s indirect outputs. We can use a Test Double as long as we have
a way to replace the DOC with the Test Double. This could be via Dependency
Injection (page 678) or by Dependency Lookup (page 686).
There are two fundamentally different ways to implement Behavior Verifi ca-
tion, each with its own proponents. The Mock Object (page 544) community has
been very vocal about the use of “mocks” as an Expected Behavior Specifi cation,
so it is the more commonly used approach. Nevertheless, Mock Objects are not
the only way of doing Behavior Verifi cation.
Variation: Procedural Behavior Verifi cation
In Procedural Behavior Verifi cation, we capture the method calls made by the SUT
as it executes and later get access to them from within the Test Method (page 348).
Then we use Equality Assertions (see Assertion Method on page 362) to compare
them with the expected results.
The most common way of trapping the indirect outputs of the SUT is to
install a Test Spy (page 538) in place of the DOC during the fi xture setup phase
of the Four-Phase Test (page 358). During the result verifi cation phase of the
test, we ask the Test Spy how it was used by the SUT during the exercise SUT
phase. Use of a Test Spy does not require any advance knowledge of how the
methods of the DOC will be called.
The alternative is to ask the real DOC how it was used. Although this scheme
is not always feasible, when it is, it avoids the need to use a Test Double and
minimizes the degree to which we have Overspecifi ed Software.
We can reduce the amount of code in the Test Method (and avoid Test Code
Duplication; see page 213) by defi ning Expected Objects (see State Verifi cation)
for the arguments of method calls or by delegating the verifi cation of them to
Custom Assertions (page 474).
Variation: Expected Behavior Specifi cation
Expected Behavior Specifi cation is a different way of doing Behavior Verifi cation.
Instead of waiting until after the fact to verify the indirect outputs of the SUT
by using a sequence of assertions, we load the Expected Behavior Specifi cation
into a Mock Object and let it verify that the method calls are correct as they are
received.
We can use an Expected Behavior Specifi cation when we know exactly what
should happen ahead of time and we want to remove all Procedural Behavior
Verifi cation from the Test Method. This pattern variation tends to make the
Also known as:
Expected
Behavior
Behavior
Verifi cation