Lesson 35 of 93 · 7 min read · Updated
Abstractions and Dependency Injection as Test Seams in C#
On this page
A seam is a place where you can change what a piece of code talks to without editing that code. In .NET the most common seam is a constructor that takes an interface. This lesson walks through a CheckoutService whose constructor openly declares four dependencies, and shows why each one is a seam that a unit test can use. The same service is the system under test for the rest of the Moq lessons.
Technical Requirements
- .NET 11 SDK
Microsoft.Extensions.Loggingabstractions (part of the shared framework in .NET 11, no extra package needed forILogger<T>)- For the test side: xUnit 2.9.3, Moq 4.21.0, Microsoft.Extensions.TimeProvider.Testing 11.0.0
The Domain Types
Before looking at the service, here are the two small value types it works with. Money is a value object with an amount and a currency. Coupon knows whether it is valid at a given moment.
M05.TestableDesign/Domain/Money.cs
M05.TestableDesign/Domain/Coupon.cs
Coupon.IsValidAt takes the instant as a parameter. It never calls DateTime.UtcNow. That one decision means a test can check an expired coupon and a valid coupon with plain values, and no fake is needed.
Narrow Interfaces at Real Boundaries
The service talks to two things outside the process: a payment provider and a coupon store. Each one is behind a narrow interface.
M05.TestableDesign/Ports/Ports.cs
These interfaces exist for a reason that has nothing to do with tests. A real payment gateway can be replaced by another provider. A coupon store can move from one database to another. The interfaces describe what the application needs from those systems, with only the methods it uses. That is why they make good seams: they sit where the system really touches something it does not control.
Avoid the opposite habit of adding an interface to every class "so that we can mock it". An interface in front of pure in-process logic, such as Coupon.IsValidAt, adds a layer and gives nothing back. Test that logic directly.
The Constructor Declares the Test Cost
Here is the service. Read the constructor first.
M05.TestableDesign/CheckoutService.cs
The primary constructor lists four dependencies. Each one maps to a seam:
| Dependency | Production | Unit test |
|---|---|---|
IPaymentGateway | HTTP client for the payment provider | Moq or NSubstitute double |
ICouponRepository | Database repository | Moq double returning a test coupon |
TimeProvider | TimeProvider.System | FakeTimeProvider at a fixed instant |
ILogger<CheckoutService> | Logger from the host | NullLogger or a recording logger |
Four parameters tell you a test has four things to arrange. That is useful information. If the list grows to eight or ten, the constructor is warning you that the class does too much, long before the test becomes painful.
Two more details matter. First, the CancellationToken flows down to every boundary call, so cancellation can be tested too. Second, the log call uses an EventId and a named placeholder {CouponCode}. A later lesson verifies the log through those two items instead of the formatted sentence.
Using the Seams in a Test
A test builds the service by passing a double for each seam. The helper below is the shared setup used by the Moq tests.
M05.TestableDesign.Tests/MoqBasicsTests.cs
The production code does not change at all. It does not know it is running in a test. The test simply passes different objects through the same constructor that the dependency injection container uses in production.
In the application host, the same four dependencies come from the container. You register the real gateway and repository, register TimeProvider.System as the singleton TimeProvider, and the logging infrastructure supplies ILogger<CheckoutService>. The test and the host use the same seam with different objects behind it.
Summary
- A seam is a place where a test can swap a collaborator without editing the code; a constructor that takes an abstraction is the usual seam in .NET.
- Put interfaces at real boundaries such as payment providers and databases, not in front of every class.
- Pass time in through
TimeProvideror as a method parameter, asCoupon.IsValidAtdoes. - The constructor parameter list is the test cost: each dependency is one thing a test must arrange.
- Flow the
CancellationTokendown and log with anEventIdand named properties so both are testable later.