Lesson 33 of 93 · 6 min read · Updated
Choosing the Lightest Test Double: A Decision Table for Dummy, Stub, Fake, Spy and Mock
On this page
The previous lessons built five test doubles by hand: dummy, stub, fake, spy and mock. Knowing them is half the job. The other half is choosing one for each collaborator in each test. This lesson turns the rules into a short decision table, applies it to the OrderService tests, and shows where the rule "pick the lightest double" stops working.
Technical Requirements
- .NET 11 SDK
- xUnit 2.9.3
- The hand-written doubles from the previous lessons. No mocking library is needed.
The Decision Table
Order the doubles from lightest to heaviest: dummy, stub, fake, spy, mock. A lighter double knows less about the code under test, so it breaks less often when that code changes. Start at the top of the table and stop at the first row that fits.
| Situation | Double | Why |
|---|---|---|
| The dependency is not used at all in this scenario | Dummy | It only fills the parameter |
| The test must set up the input of the scenario | Stub | It gives a canned answer to a query |
| A state change is part of the scenario | Fake | A simplified but working implementation |
| You want to record what happened and check it later | Spy | It keeps the Arrange, Act, Assert flow intact |
| An outgoing effect that cannot be observed otherwise | Mock | It verifies its expectation itself |
Each row asks a question about the scenario, not about the interface. The same IInventory can be a stub in one test and a fake in another. The choice belongs to the test.
Applying the Table to One Test
The Arrange block of the first Dummy and Stub test applies the table to all four collaborators, and its comments name the choice for each.
M04.TestDoubles.Tests/DummyAndStubTests.cs
Walk through the table for each line:
IAuditLog: the test never looks at the log, so the first row fits. Dummy.IInventory: the scenario needs "five keyboards in stock" as input, and nothing checks the stock afterwards. Second row. Stub.IOrderNumberGenerator: the test asserts on the exact order number, so it needs a fixed answer. Stub.IEmailSender: this test does not check the email at all, so a dummy would be enough. The spy is used because it is already at hand and does nothing harmful. Its record is simply never read.
The last point is worth noting. Using a heavier double than needed is not a bug, but it can mislead a reader into thinking the email matters in this test. When the double is only there to fill a slot, a comment like the one above removes the doubt.
Where "Lightest" Is Not Enough
The table says to stop at the first row that fits. The key word is "fits". The fake versus stub test from the Fake and Spy lesson shows a case where the lighter double does not fit.
M04.TestDoubles.Tests/FakeAndSpyTests.cs
The scenario is "the second order is rejected because the first one used the last item". That is a state change, so it lands on the third row. A StubInventory would silently swallow the first Reserve call and report one item again, and the second order would succeed. When the scenario needs state, a stub is not enough, however light it is.
Common mistake: jumping straight to the bottom row. Many teams create a mock for every interface by default, because a library makes it one line of code. The result is tests that verify calls on stubs and dummies, which the table never asks for. Those tests break on every refactoring. Use the mock row only for outgoing effects such as email, messages or payment calls.
Questions to Check Your Understanding
Use these questions to test the rules against the code:
IAuditLogis a dummy in every test so far. In which test would it become a spy? Answer: in a test whose scenario requires an audit entry, for example a rule that every rejected order must be logged. Then the log entry is an outgoing effect the business cares about.SpyEmailSenderandMockEmailSenderseem to do the same job. Where is the difference? Answer: in when the expectation is written. The spy is checked after Act; the mock gets its expectation before Act and verifies itself.- In
Place_VerifiedThreeWaysthere are three kinds of assertion. Which is the most resistant to refactoring and which is the most fragile? Answer: the output check is the most resistant, the state check is close behind, and the behaviour check is the most fragile. - Why does the
OrderService.Totaltest use no double? Answer:Totalis a pure function with no collaborators. That tells you a design lesson: logic separated from I/O needs no doubles at all.
Summary
- Order the doubles from lightest to heaviest: dummy, stub, fake, spy, mock. Lighter doubles know less and break less.
- Pick per collaborator and per test, by asking what role the dependency plays in this scenario.
- Stop at the first row that fits, but only if it really can express the scenario. A scenario that needs state needs a fake, not a stub.
- Use spies and mocks only for outgoing effects the business cares about, never for queries or internal steps.
- Writing the doubles by hand once shows what a mocking library generates. The same decision table applies when a library creates them for you.