Lesson 33 of 93 · 6 min read · Updated

Choosing the Lightest Test Double: A Decision Table for Dummy, Stub, Fake, Spy and Mock

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

  1. .NET 11 SDK
  2. xUnit 2.9.3
  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.

SituationDoubleWhy
The dependency is not used at all in this scenarioDummyIt only fills the parameter
The test must set up the input of the scenarioStubIt gives a canned answer to a query
A state change is part of the scenarioFakeA simplified but working implementation
You want to record what happened and check it laterSpyIt keeps the Arrange, Act, Assert flow intact
An outgoing effect that cannot be observed otherwiseMockIt 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

// Arrange
// • IInventory → STUB: sets up the "stock available" scenario.
// • IOrderNumber→ STUB: returns a deterministic number.
// • IEmailSender→ SPY : we do not verify it here, but we need something.
// • IAuditLog → DUMMY: has nothing to do with the scenario.
var inventory = new StubInventory(new Dictionary<string, int> { ["KLAVYE-01"] = 5 });
var numbers = new StubOrderNumberGenerator("SIP-2026-0001");
var email = new SpyEmailSender();
IAuditLog dummyAudit = new DummyAuditLog();

var sut = new OrderService(inventory, numbers, email, dummyAudit);

Walk through the table for each line:

  1. IAuditLog: the test never looks at the log, so the first row fits. Dummy.
  2. IInventory: the scenario needs "five keyboards in stock" as input, and nothing checks the stock afterwards. Second row. Stub.
  3. IOrderNumberGenerator: the test asserts on the exact order number, so it needs a fixed answer. Stub.
  4. 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

// What a FAKE gives us: STATE.
// We could not set up this scenario with a stub, because a stub swallows the reservation.
var inventory = new FakeInventory(new Dictionary<string, int> { ["MOUSE-01"] = 1 });
var sut = new OrderService(
inventory,
new StubOrderNumberGenerator("SIP-1"),
new SpyEmailSender(),
new DummyAuditLog());

var first = sut.Place("ali@ornek.com", [new OrderLine("MOUSE-01", 1, 250m)]);
var second = sut.Place("ali@ornek.com", [new OrderLine("MOUSE-01", 1, 250m)]);

Assert.True(first.IsSuccessful);
Assert.False(second.IsSuccessful);

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:

  1. IAuditLog is 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.
  2. SpyEmailSender and MockEmailSender seem 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.
  3. In Place_VerifiedThreeWays there 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.
  4. Why does the OrderService.Total test use no double? Answer: Total is a pure function with no collaborators. That tells you a design lesson: logic separated from I/O needs no doubles at all.

Summary

  1. Order the doubles from lightest to heaviest: dummy, stub, fake, spy, mock. Lighter doubles know less and break less.
  2. Pick per collaborator and per test, by asking what role the dependency plays in this scenario.
  3. 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.
  4. Use spies and mocks only for outgoing effects the business cares about, never for queries or internal steps.
  5. Writing the doubles by hand once shows what a mocking library generates. The same decision table applies when a library creates them for you.