Field Guide to Mocks – The Code
Agile
in a Flash by Jeff Langr and Tim Ottinger (card #49)
> Test double: Any object that
emulates another
> Stub: Returns a fixed value
to the SUT
> Fake: Emulates a production
object
> Mock: Self-verifies
> Partial mock: Combines
production and mock methods
> Spy: Records messages for
later verification
--
Test doubles are emulation
objects you build to simplify testing. Surrounding the System Under Test (SUT)
with test doubles allows you to exercise and observe it in isolation.
Otherwise, accessing database, network, external hardware, or other subsystems
can slow or break the test run and cause false positives. Stubs, fakes, mocks
(partial or not), and spies are nuanced kinds of test doubles. This field
guide, based on XUnit Test Patterns,
will help you sort out standard terminology (used in many mock tools).
Test doubles have changed the art
of test-driving by allowing tests to be smaller, simpler, and faster running.
They can allow you to have a suite of thousands of tests that runs in a minute
or less. Such rapid feedback gives you the confidence to dramatically change
your code every few minutes. But take care; you should do the following:
> Learn to use test doubles,
but employ them only when you need the isolation.
> Use a mock tool (instead of
hand-coding them) if it improves test quality.
> Learn the various types of
mocks summarized in this field guide.*12
> Read Card 51, Test
DoubleTroubles, to avoid their many pitfalls.
--
12. If only to avoid being
embarrassed by your peers. Or just keep this card handy.
"The secret of life is
honesty and fair dealing. IF you can fake that, you've got it made."
Groucho Marx
No comments:
Post a Comment