Lesson 28 of 93 · 6 min read · Updated
Test Doubles in .NET: Controlling a Class's Collaborators in Unit Tests
On this page
A unit test should check one piece of logic, but most real classes do not work alone. They call a database, a counter, an email server or a log. A test double is an object that stands in for one of those collaborators so the test controls what the class sees and can observe what it does. This lesson introduces the order service used across this series and shows the four collaborator boundaries it has, and why each one calls for a different kind of double.
Technical Requirements
- .NET 11 SDK
- xUnit 2.9.3
- No mocking library. Every double in this series is written by hand, so you see exactly what a library such as Moq generates for you later.
The System Under Test: OrderService
The class we test places an order. It checks stock, reserves it, gets an order number, sends a confirmation email and writes an audit entry. It takes all four collaborators through its primary constructor, which means a test can pass in any implementation it wants.
M04.TestDoubles/OrderService.cs
The four interfaces are the seams of this class. A seam is a place where you can swap one implementation for another without editing the code under test. In production, a dependency injection container passes the real database-backed inventory and the real SMTP sender. In a test, you pass small classes that you write yourself. The service cannot tell the difference, and that is the whole point: the logic inside Place runs exactly as it would in production, but every call that leaves the class lands on an object the test owns.
The method returns an OrderResult instead of throwing for business failures. That matters for testing: a returned value is the easiest thing to assert on.
M04.TestDoubles/Order.cs
Four Boundaries, Four Different Roles
Each collaborator sits behind a small interface. Look at what each one does for the service, not at what it is called. The role decides which double fits.
M04.TestDoubles/Ports/IInventory.cs
IInventory has a query (AvailableQuantity) and a command (Reserve). The query feeds input into the scenario: "is there stock?" The command changes state. If your test only needs the answer to the query, a double that returns a fixed value is enough. If the test needs the stock to really go down, the double must keep state.
M04.TestDoubles/Ports/IOrderNumberGenerator.cs
A real generator would hand out a new number each time. Tests need a known value so they can assert on it. A double that always returns the same string makes the test deterministic.
M04.TestDoubles/Ports/IEmailSender.cs
SendOrderConfirmation returns nothing and changes nothing the test can read. The only way to know an email was sent is to record or check the call itself. This is the one boundary where verifying an interaction is the right tool.
M04.TestDoubles/Ports/IAuditLog.cs
The audit log does not affect whether the order succeeds. Most tests only pass something because the constructor demands it.
Mapping Roles to Doubles
Putting it together gives a simple map. This series covers each row in its own lesson.
| Collaborator | Role in the test | Typical double |
|---|---|---|
IAuditLog | Required by the constructor, not part of the scenario | Dummy |
IInventory | Supplies input, sometimes needs real state | Stub or Fake |
IOrderNumberGenerator | Must return a known, repeatable value | Stub |
IEmailSender | Outgoing effect with no return value | Spy or Mock |
Notice the method Total. It is static and uses no collaborator at all, so its test needs no double. When a piece of logic can be pulled out into a pure function like this, the test gets simpler. Keep that in mind: the best double is often no double.
A common mistake is to pick one double type and use it everywhere, for example mocking every interface and verifying every call. That ties the test to the internal steps of the method. If you later reorder the stock check or cache a query, the test breaks even though the behaviour the customer sees is the same. Choosing per boundary avoids that.
Summary
- A test double replaces a collaborator so the test controls the inputs and can observe the outputs of the class under test.
OrderServicehas four collaborators, and each one plays a different role: unused, input provider, deterministic value, outgoing effect.- The role of the collaborator, not its name, decides which double to use.
- Queries feed input; commands change state; outgoing calls with no return value are the only place where interaction checks make sense.
- Pure logic such as
Totalneeds no double at all. The next lesson starts with the two simplest doubles: Dummy and Stub.