Lesson 46 of 93 · 5 min read · Updated
Over-Mocking: Why Verifying Every Interaction Makes Unit Tests Fragile
On this page
A mocking library makes it easy to verify every call your code makes. That ease is a trap. When a test pins down each internal interaction, a harmless refactoring turns into a red build, and the team learns to distrust the tests. This lesson compares two tests of the same checkout behavior. Both pass today. One checks the observable outcome and survives refactoring. The other over-specifies the implementation and breaks the moment someone adds a cache.
Technical Requirements
- .NET 11 SDK
- xUnit 2.9.3
- Moq 4.21.0
- FluentAssertions 7.2.2
- Microsoft.Extensions.TimeProvider.Testing 11.0.0
Shared Setup
Both tests use the same arrangement: a gateway that approves everything and a repository that returns a valid 10 percent coupon.
M05.TestableDesign.Tests/OverMockingTests.cs
The coupon expires 30 days after the fake clock's "now", so it is valid in every run regardless of the real date.
The Good Test: Assert the Outcome
M05.TestableDesign.Tests/OverMockingTests.cs
This test checks two things a customer would notice. The checkout succeeds and charges 900 after a 10 percent discount. And the card is charged exactly once, for that amount. The capture is verified because it is a side effect with no other trace in the result: if it did not happen, the shop would not get paid, and if it happened twice, the customer would pay twice.
Everything else is left free. The test does not care how the coupon is found, how many times the repository is asked, or in which order internal steps happen.
The Bad Test: Freeze Every Interaction
M05.TestableDesign.Tests/OverMockingTests.cs
Look at what this test checks and what it misses.
- It verifies that
FindAsyncis called exactly once. Add a coupon cache and the second checkout in a session no longer calls it. The test breaks; the user sees no change. - It verifies that
AuthorizeAsyncis called once. That is already implied by a successful result, so the line adds coupling and no protection. - It calls
VerifyNoOtherCalls()on both mocks. Add a harmless call, such as a status check before capture, and the test breaks. - It does not check the result or the captured amount. If the discount were calculated wrongly and the customer was charged 1000, every line would still pass.
So the bad test is both too strict and too weak. It fails on safe changes and passes on real bugs.
There is also a cost over time. Each false failure trains developers to update tests mechanically until they turn green. After a few rounds, nobody reads why a mock test failed, and the suite stops catching real regressions. A test earns trust only when its failures mean something.
The Filter Question
For each verification in a test, ask one question:
"If this verification fails, has something broken for the user?"
Apply it to the bad test. A different number of FindAsync calls: no. A missing AuthorizeAsync verification: no, the result already covers it. An extra call caught by VerifyNoOtherCalls: usually no. A capture with the wrong amount: yes, but the bad test does not check the amount. If the answer is no, delete the verification. If the answer is yes, make sure the test really checks it.
When Interaction Checks Are Right
Over-mocking does not mean "never verify". Verify interactions that are themselves the behavior: money captured once, an e-mail sent, a message published, no database call when the input is invalid. These are outcomes seen from outside the class, even though they happen through a dependency. What you should avoid is verifying the internal steps that lead to the outcome. Those steps belong to the implementation, and the implementation must be free to change.
Summary
- Tests that verify every internal call break on safe refactorings such as adding a cache.
- Prefer asserting the observable result; verify a dependency call only when the call is the behavior.
- Over-specified tests are often also weak: they can miss wrong values while checking call counts.
- Use
VerifyNoOtherCalls()and exact call counts on queries only when they are part of the contract. - Ask of each verification: "If this fails, has something broken for the user?" If not, delete it.