When Your Mock Lies to You: The Contract Drift Problem

Mocking a dependency is an unambiguous productivity win right up until the mock and the real thing quietly stop agreeing with each other. At that point, the mock isn't just unhelpful — it's actively worse than not having one, because it gives you false confidence. Your tests pass, your demo works, everything looks fine, and then the real integration breaks in a way none of your mocked tests would ever have caught, because the mock was answering a question the real API stopped asking six weeks ago.
This is worth saying plainly in an article that's otherwise pro-mocking: a mock is a model of a real system, and like any model, it's only useful for as long as it stays accurate. Nobody sets out to let that happen — it drifts in gradually, through a series of individually reasonable decisions.
How drift actually happens
A few common, unglamorous ways a mock quietly falls out of sync with reality:
- The real API adds a field, and the mock doesn't. Your frontend never notices in tests, because it was never told to expect the field. It notices in production, when a new UI element that reads that field renders
undefined. - The real API changes a status code or error shape, and nobody updates the mock because nobody remembered the mock existed for that endpoint.
- The mock was hand-built once, early on, and the real API evolved several versions past it while the mock stayed exactly as it was on day one.
- Different team members maintain slightly different local mocks of the same dependency, each drifting independently, so "works on my machine" stops meaning anything consistent.
None of these require anyone to be careless. They're just what naturally happens to any artifact that isn't automatically checked against the thing it's supposed to represent.
What actually limits the damage
A few practices meaningfully reduce how much this costs you, without requiring you to give up mocking as a technique:
- Generate the mock from the same source of truth the real API is supposed to honor, rather than hand-writing it independently. If both the mock and (ideally) the real implementation are checked against the same OpenAPI spec, a change to the contract is a change in one place, not two things that need to be remembered to move in lockstep. Importing a spec instead of manually retyping routes is the mechanical version of this.
- Treat "the mock disagrees with the real API" as a bug to fix, not a mock to quietly patch around. If integration testing (even occasional, manual integration testing against the real thing) reveals the mock was wrong, that's a signal the contract itself needs updating everywhere it's represented — not just in the one place someone happened to notice.
- Don't mock everything, all the time. Mocking is for the specific things you need to control deliberately — a hard-to-trigger error state, a dependency you don't own, a backend that doesn't exist yet. A dependency you fully control and that's cheap to run for real in CI often doesn't need a mock standing in for it at all; every mock you maintain is one more thing that can drift.
- Periodically run your real client code against the real API, even if most day-to-day development and testing happens against a mock. This doesn't need to be constant — it needs to happen often enough that drift gets caught in weeks, not discovered by a customer months later.
Mocking isn't the mistake here. Treating a mock as a permanent, unmaintained fixture instead of a model that needs to stay honest is. The moment "the mock says it works" and "it actually works" can diverge without anyone noticing, you've traded a slow integration problem for a fast, false sense of security — which is a worse trade.
Xerat02
Building MockBase.
More from the blog

Three Ways to Fake a Dependency, and When Each One Lies to You
Mocking, service virtualization, and contract testing all answer "what do I do about a dependency I don't control" — they answer it for different problems, and using the wrong one gives you a different flavor of false confidence each time.

Where Should Your Mock Actually Live?
Intercepting a request in your app's own code and intercepting it on the network are two different techniques with different tradeoffs — and most teams pick one out of habit rather than fit.