
90
Chapter 8 Transient Fixture Management
constructor method signatures or semantics. When a test does not care about
the specifi c identity of the objects it is creating, we can use Anonymous Cre-
ation Methods (see Creation Method). These methods generate any unique keys
required by the object being created. By using a Distinct Generated Value (see
Generated Value on page 723), we can guarantee that no other test instance
that requires a similar object will accidentally use the same object as this test.
This safeguard prevents many forms of the behavior smell Erratic Test, includ-
ing Unrepeatable Tests, Interacting Tests, and Test Run Wars, even if we hap-
pen to be using a persistent object repository that supports Shared Fixtures.
When a test does care about the attributes of the object being created, we
use a Parameterized Anonymous Creation Method (see Creation Method). This
method is passed any attributes that the test cares about (i.e., attributes that are
important to the test outcome), leaving all other attributes to be defaulted by the
implementation of the Creation Method. My motto is this:
When it is not important for something to be seen in the test method,
it is important that it not be seen in the test method!
Delegated Setup is often used when we write input validation tests for
SUT methods that are expected to validate the attributes of an object argu-
ment. In such a case, we need to write a separate test for each invalid at-
tribute that should be detected. Building all of these slightly invalid objects
would be a lot of work using In-line Setup. We can reduce the effort and
the amount of Test Code Duplication dramatically by using the pattern
One Bad Attribute (see Derived Value on page 718). That is, we first call
a Creation Method to create a valid object, and then we replace one attri-
bute with an invalid value that should be rejected by the SUT. Similarly, we
might create an object in the correct state by using a Named State Reaching
Method (see Creation Method).
Some people prefer to Reuse Tests for Fixture Setup (see Creation Method) as an
alternative to using Chained Tests (page 454). That is, they call other tests directly
within the setup portion of their test. This approach is not an unreasonable one as
long as the test reader can readily identify what the other test is setting up for the
current test. Unfortunately, very few tests are named in such a way as to convey this
intention. For this reason, if we value Tests as Documentation, we will want to con-
sider wrapping the called test with a Creation Method that has an Intent-Revealing
Name so that test reader can get a sense of what the fi xture looks like.
The Creation Methods can be kept as private methods on the Testcase
Class, pulled up to a Testcase Superclass (page 638), or moved to a Test Help-
er (page 643). The “mother of all creation methods” is Object Mother (see Test