StoicBuilder

HandbookVIII the PalaestraMocks, stubs and fakes

VIIIthe Palaestra, Test it

Mocks, stubs and fakes

1 min readUpdated 28 November

In plain words

A test double replaces a real dependency during a test: a stub returns a canned answer, a mock records how it was called, and a fake is a small working version, like an in-memory database.

Where you see it

Harbor's upgrade flow asks Stripe to charge the team's card. In the test, a stand-in payments client answers with a successful charge or a declined one, so the test runs offline in milliseconds and nobody pays anything.

lib/billing/upgrade.test.tsterminal
const payments = { charge: vi.fn().mockResolvedValue({ status: "declined" }) }; const result = await upgradePlan({ teamId: "t_1", plan: "pro" }, { payments }); expect(result.ok).toBe(false);expect(payments.charge).toHaveBeenCalledWith({ teamId: "t_1", amountCents: 3000 });
The stub decides what the payment service says. The last line uses it as a mock, checking what your code asked for.

What goes wrong

  1. Mocking the thing you meant to test

    If the test replaces the function that calculates the price, it only checks that the stand-in returns what you told it to. Replace what sits at the edges, such as Stripe, email and the clock, and run your own logic for real.

  2. A stand-in that no longer matches the real service

    Your stub returns { status: "failed" }, while Stripe actually reports a decline as an error with a code such as card_declined. The tests stay green and production breaks. Copy stub responses from the provider's docs or a real test-mode call.

  3. Stand-ins that leak between tests

    A module-level stub set to decline in one test is still declining in the next, so results depend on the order tests run in. Reset them between tests, for example with vi.resetAllMocks() in afterEach.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
In these tests, replace only the outside services (Stripe, the email sender and the clock) with test doubles, and run Harbor's own billing logic for real. Base each stub's response on a real response from the provider's docs, and reset every mock between tests.

Quick check

Sign in to earn +10 XP for it

A test for Harbor's upgrade flow mocks Stripe, the database and the calculatePrice function. It passes. What has it proved?

Got a prompt that touches Mocks, stubs and fakes?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.