Lesson 30 of 93 · 6 min read · Updated
Fake and Spy Test Doubles in C#: Working State and Recorded Calls
On this page
A stub hands back fixed answers and forgets everything else. Some scenarios need more: the stock must really go down after an order, or the test must see which emails were sent. This lesson adds two doubles that do real work. A fake is a simplified but working implementation, and a spy records the calls it receives so the test can inspect them afterwards. Both are written by hand for the same OrderService.
Technical Requirements
- .NET 11 SDK
- xUnit 2.9.3
- No mocking library: the fake and the spy are plain C# classes.
Fake: A Working, Simplified Implementation
FakeInventory keeps stock in a dictionary. Reserve subtracts from it, and asking for more than is available throws, just as a real store would refuse. It does not talk to a database, so tests stay fast, but it behaves like a real inventory.
M04.TestDoubles.Tests/Doubles/FakeInventory.cs
Two details matter. The constructor copies the initial dictionary, so the test's own dictionary is never changed behind its back. And Reserve refuses invalid requests instead of letting stock go negative. A fake that accepts impossible states would hide bugs in the code under test.
A fake is code, so it can be wrong. Keep it small and give it the same rules as the real implementation. If the real store and the fake drift apart, your tests pass against behaviour that production does not have. For larger fakes, some teams run the same contract tests against both the fake and the real implementation.
The Test a Stub Cannot Write
Place the same order twice when only one item is in stock. The first order should succeed and the second should fail.
M04.TestDoubles.Tests/FakeAndSpyTests.cs
Try this with StubInventory. Its Reserve does nothing, so AvailableQuantity keeps returning 1 and the second order succeeds too. The test would fail, not because the service is wrong, but because the double cannot represent the scenario.
This is the limit of the rule "use the lightest double". A stub is lighter than a fake, but when the scenario depends on state changing between calls, a stub is not enough. Pick the lightest double that can still express the scenario.
The last assertion reads the state of the fake directly: zero items left. That is a state check, and it survives refactoring well. It does not care how the service reserved the stock, only that the stock is gone.
Spy: Record Now, Check Later
IEmailSender returns nothing, so the test cannot see its effect through a return value. A spy solves this by keeping a list of every call it receives.
M04.TestDoubles.Tests/Doubles/SpyEmailSender.cs
The spy exposes its record as IReadOnlyList, so a test can read it but cannot change it by accident. Each entry is a named tuple, which makes assertions read like plain sentences.
M04.TestDoubles.Tests/FakeAndSpyTests.cs
Assert.Single does two jobs: it fails if the list does not have exactly one item, and it returns that item for the next assertions. The test checks the recipient and the order number, which are the facts the customer cares about.
Because the spy only records, the test keeps a clean Arrange, Act, Assert shape. Nothing about the expected email appears before the Act line. You read the setup, then the action, then what should have happened.
Checking That Something Did Not Happen
A spy also proves that a call did not happen. When stock is missing, the customer must not get a confirmation email.
M04.TestDoubles.Tests/FakeAndSpyTests.cs
Negative checks like this catch a real class of bugs: a refactoring that moves the email call above the stock check would send a confirmation for an order that never existed.
Common mistake: spying on everything. Use a spy for outgoing effects that matter to the business, such as emails or published messages. Recording every call to the inventory and asserting on that list would tie the test to internal steps, which is exactly what the stub rule from the previous lesson warns against.
Summary
- A fake is a simplified implementation with real behaviour, such as an in-memory store that really decrements stock.
- Use a fake when the scenario depends on state changing between calls. A stub swallows commands and cannot express that.
- A spy records the calls it receives. The test checks the record in its Assert section, so the Arrange, Act, Assert flow stays readable.
- A spy can also prove that an outgoing call did not happen, which is part of the contract too.
- Spy only on outgoing effects that matter to the business. The next lesson shows the mock, which knows its expectation in advance.