Lesson 34 of 93 · 6 min read · Updated
Hidden Dependencies: Why Some .NET Classes Cannot Be Unit Tested
On this page
How hard a class is to test depends almost entirely on its dependencies. This lesson compares two classes that compute the same night-time discount. One reads the system clock, a static Random and the console from the inside. The other receives time and randomness from the outside. The first one produces a test that protects almost nothing. The second one needs no mocking library at all.
Technical Requirements
- .NET 11 SDK
- xUnit 2.9.3
- FluentAssertions 7.2.2
- Microsoft.Extensions.TimeProvider.Testing 11.0.0 (for
FakeTimeProvider)
The Hard-to-Test Version
The business rule is simple. Between 22:00 and 06:00 the price drops by 20 percent, minus a small random "jitter" of up to 4 units. Here is the first implementation.
M05.TestableDesign/Legacy/HardToTestPriceService.cs
The class has no constructor parameters, so it looks easy to use. That is the trap. The constructor tells you nothing about what the method really depends on. To know the real dependencies you must read the body, and you cannot change any of them from a test:
DateTime.UtcNowreturns whatever the machine clock says. You cannot ask for 23:00.static Randomproduces a different number on every call and is shared by every caller.Console.WriteLinewrites to a global stream. The output leaves the class and is lost.
Each of these is a hidden dependency: something the code needs but does not ask for.
What the Test Looks Like
Try to write a test for the legacy class. You do not know what time it will be when the test runs. You do not know which random number will come out. So the only honest assertion is a range wide enough to cover every case.
M05.TestableDesign.Tests/TimeProviderTests.cs
BeInRange(75m, 100m) passes when the discount is applied and when it is not. It passes when the jitter is 0 and when it is 4. If someone breaks the night window, for example by changing >= 22 to > 22, this test still passes. Worse, a test that runs at 23:00 on a CI server and at 14:00 on your laptop exercises two different code paths. That is how flaky tests are born.
When a test can only assert something this vague, the problem is not the test. The problem is the design of the class under test.
The Testable Version
The second implementation does the same work. The difference is that every dependency arrives through the constructor.
M05.TestableDesign/TestablePriceService.cs
Three changes make the difference:
- Time comes from
TimeProvider, the abstraction built into .NET. In production you passTimeProvider.System. In a test you pass aFakeTimeProviderset to any instant you like. - Randomness is a
Func<int>. Production can pass() => Random.Shared.Next(0, 5). A test passes() => 3. - The console write is gone. If the class needs to report something, it should return it or take an
ILogger<T>, which a test can observe.
Now the constructor is honest. It lists exactly what the class needs, and each item is something a test can control.
Tests Without Any Mocking Library
Because the dependencies are explicit, the tests are exact.
M05.TestableDesign.Tests/TimeProviderTests.cs
The assertion is a single value: 77m. If the formula changes, the test fails. If the night window changes, a test at the boundary hour fails. The test gives the same result on every machine at every time of day.
Notice that no mock library appears here. FakeTimeProvider is a ready-made fake from Microsoft, and the jitter is a lambda. Good design often removes the need for mocks instead of making mocking easier.
Common Mistake
A common reaction to a class like HardToTestPriceService is to keep the design and fight the test: run it only at night, retry it until it passes, or widen the assertion until it is always green. All three hide the real issue. The fix belongs in the production code: make each hidden dependency a visible constructor parameter.
Summary
- Hidden dependencies such as
DateTime.UtcNow,static Random,new HttpClient()andConsole.WriteLinecannot be controlled from a test. - A test against hidden dependencies can only assert vague ranges, so it protects almost nothing and can be flaky.
- Moving each dependency to the constructor makes the class honest about what it needs.
- With
TimeProviderand aFunc<int>, the same logic is tested with exact values and no mocking library. - If you cannot write a meaningful test, change the design, not the test.