The mock that lied

Green unit tests against a provider that no longer exists

L216 MINTESTING
PREDICT

The billing team renames amount to total and deploys. Your checkout's unit tests use a hand-written mock of the billing response. What do your tests do?

Your checkout calls a billing API. To keep its tests fast, it replaces the API with a mock: a response you typed by hand. The tests check the checkout against your mock. They say nothing about the real API, and nothing tells you when the two part ways.

When the provider renames a field, changes a type or starts returning a new status, your mock stays exactly as it was. The suite stays green and production breaks. A contract closes the gap. The consumer writes down what it reads, and the provider’s pipeline checks its real response against that list.

▶ SIMULATION
CONSUMER UNIT TESTS
mock: amount: number
runs: —
REAL PROVIDER RESPONSE
id: "inv_101"
amount: 48200
status: "paid"
PRODUCTION CHECKOUT
last call: —
deploys blocked: 0
last—
✓ no production call fails while the consumer's unit tests are green
SCHEDULE
01[CONSUMER]unit tests run against the hand-written mock
02[PROVIDER]provider renames amount to total
03[PROVIDER]provider deploys (its own tests pass)
04[CONSUMER]unit tests run again: the mock has not changed
05[CONSUMER]production checkout calls the real API
06[CI]report
Start of break: the provider renames a field scenario. 6 steps.
// The consumer's test owns the mock. Nothing ties it to the provider.
const mockBilling = { id: 'inv_101', amount: 48200, status: 'paid' };

test('shows the amount', () => {
  const page = render(mockBilling);
  expect(page.total).toBe('₦482.00');   // green forever
});

Watch the invariant strip. In the first three scenarios the unit tests are green throughout. In FIX, the pipeline stops the rename, lets the added field through, and production keeps working.

BREAK IT NOT YET

The provider is about to deploy a change with no contract check in its pipeline. Insert one change before the deploy so the consumer's unit tests stay green and the production checkout breaks.

GOAL: unit tests green, production call fails

FIX IT NOT YET

Write verifyContract(contract, response). The contract maps each field the consumer reads to 'string', 'number', 'boolean' or { enum: [...] }. Return an array of problem strings: empty when the response satisfies the contract. A field that is missing, has the wrong type, or holds a value outside its enum is a problem. Extra fields are fine: the provider may add what the consumer does not read. (A new status is caught here only because this contract declares the enum; a Pact-style contract covers just the examples the consumer recorded.)

A contract is not an integration test. It checks shape: field names, types, allowed values. It does not check that the provider’s numbers are right. It also only protects the fields the consumer lists, which is the point: the consumer can say “I read these three” and the provider stays free to change the rest. For how a retry changes what a client sees from an API, see idempotency.

Mental model:

A mock is a claim about the provider that nothing checks. Turn the claim into a contract the consumer publishes and the provider verifies before every deploy, and drift becomes a red build on the provider’s side, before any customer sees it. Check what the consumer reads, and allow extra fields.

IN PRODUCTION

Microservices with separate teams, mobile apps calling a backend, third-party APIs you wrap in a client. Tools such as Pact publish consumer contracts and verify them in the provider's CI. For a third-party API you cannot gate, run the same check on a schedule against the live response.

IN INTERVIEWS

“Your unit tests pass but the service breaks in staging after another team's release. What happened and how do you stop it?” Name the hand-written mock, explain consumer-driven contracts verified in the provider's pipeline, say why extra fields must pass, and say what you still need integration tests for.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: 100% coverage, bug intact →