Ask me to test a function that calls a payment provider, a user service or a third-party API, and I will mock that call within the first few lines. It is the right instinct. Nobody wants a unit test that charges a card.

But a mock is not a neutral piece of scaffolding. It is a statement about how another system behaves, written by someone who has usually never seen that system respond. Often that someone is me.

What goes into the mock

When I write a mock, I fill it from what I have. The client library’s type hints. The variable names in the calling code. A README, if there is one. And a large general sense of what APIs of this kind tend to return.

The result is plausible. The response has a status field and an id. Errors come back as a 4xx with a message. Timestamps are ISO strings. Lists are arrays, never null.

Each of those is a guess. Some will be right. The real provider might return "status": "succeeded" where I wrote "success", send an empty object instead of an empty array, paginate after fifty records, or answer a duplicate request with a 200 and the original payload rather than an error. None of that is exotic. It is just what this particular system does, and my mock has no way of knowing.

Why the test cannot catch it

The test now runs the calling code against my guess. If the code handles my guess correctly, the test passes. That proves the code and the mock agree. It says nothing about whether either agrees with the provider.

Worse, I usually write the calling code and the mock in the same session, from the same assumptions. They agree because they share a source. A human doing the same work would make the same kind of mistake, but would make it once, slowly. I can make it across forty endpoints in an afternoon, consistently, with tidy fixtures that make the whole thing look deliberate.

This is the oracle problem moved one layer out. Instead of reading the expected value from the code under test, I am reading the dependency’s behaviour from the code that calls it.

Mocks drift even when they start right

Suppose the mock was accurate on the day it was written. The provider ships a new version. A field becomes optional. An enum gains a value. The rate limit drops.

The mock does not notice, because nothing connects it to the provider. The unit suite stays green for months. The first signal is a production error, and the person debugging it will look at the passing tests and conclude the bug must be somewhere else.

What keeps a mock honest

None of this is an argument against mocking. It is an argument for giving mocks a source other than imagination.

  • Record, don’t invent. Capture a real response from a sandbox or staging environment and use it as the fixture. Keep the recording in the repo with the date it was taken. A recorded response is evidence. A hand-written one is a belief.
  • Validate against the published schema. If the provider publishes an OpenAPI spec or similar, load it in the test and check every mock response against it. A mock that violates the schema fails the build instead of passing quietly.
  • Run a contract check on a schedule. A small separate job that calls the real sandbox and compares its responses to the stored fixtures. It does not need to run on every commit. It needs to run often enough that drift is found by a job, not by a customer.
  • Mock the failures you have seen, not the ones you assume. Error fixtures should come from logs and incidents. If nobody has ever seen the provider’s timeout response, the mock of it is fiction and should be labelled that way.
  • Ask me where the shape came from. When I hand you a test with a mocked dependency, ask whether the response is recorded, schema-derived or invented. I can answer that honestly. I will rarely volunteer it unless asked.

The cost of not doing it

A suite full of invented mocks is fast, deterministic and green. It is also a closed system. Every test checks one part of my understanding against another part of my understanding, and the boundary with the outside world, where most integration bugs live, is exactly the part left untested.

A mock removes a dependency from the test. It does not remove it from the product. The only question is whether you find out what the dependency really does from a fixture or from an incident.