HTML and CSS Reference
In-Depth Information
The terminology used in this chapter is mostly adapted from Gerard Meszaros
book “xUnit Test Patterns,” [7] slightly adjusted to the world of JavaScript. In
addition to the names and definitions of different types of test doubles, I will use
“system under test” to describe the code being tested.
16.1.1 Stunt Doubles
Gerard Meszaros compares test doubles to Hollywood's stunt doubles. Some movie
scenes require dangerous stunts, physically demanding feats or other behavior that
the leading actor is either not willing or able to perform. In such cases, a stunt
double is hired to do the job. The stunt double need not be an accomplished actor,
he simply needs to be able to catch on fire or fall off a cliff without being mortally
wounded; and he needs to look somewhat like the leading actor, at least from a
distance.
Test doubles are just like stunt doubles. They take on the job when it's incon-
venient to use the leading star (production code); all we require from them is that
the audience (system under test) cannot tell it apart from the real deal.
16.1.2 Fake Object
The stubs we've been using aggressively throughout the example projects in Part III,
Real-World Test-Driven Development in JavaScript, are one form of test doubles.
They appear to behave like real objects, but their actions are pre-programmed to
force a certain path through the system under test. Additionally, they record data
about their interaction with other objects, available in the test's verification stage.
Another kind of test double is the fake object . A fake object provides the same
functionality as the object it replaces and can be seen as an alternative implementa-
tion, only its implementation is considerably simpler. For example, when working
with Node.js the file system can easily become inconvenient from a testing perspec-
tive. Constantly accessing it can make tests slower, and keeping a lot of test data
on disk requires cleanup. We can alleviate these problems by implementing an in-
memory file system that supports the same API as Node's fs module and use this
in tests.
Fakes differ from stubs in that stubs are usually created and injected into the
system from individual tests on a per-need basis. Fakes are more comprehensive
replacements, and are usually injected into the system as a whole before running
any tests. Tests are usually completely unaware of the fakes because they behave
just like the objects they mirror, only significantly simplified. In the Node.js file
system example we can imagine a complete implementation of the fs module as
 
Search WWH ::




Custom Search