Blog

When a Fake API Needs to Remember Things

September 6, 2026Xerat024 min read
Share
When a Fake API Needs to Remember Things

The simplest possible API mock is a fixed request/response pair: hit GET /users/1, always get back the same JSON. That covers a surprising amount of frontend development — most UI screens just need some realistic-looking data to render against, and it genuinely doesn't matter whether that data is "real" in any persistent sense.

It stops covering enough the moment the feature you're building depends on cause and effect: create a todo item, then see it appear in the list. Update a user's name, then reload the profile page and see the new name, not the old one. Delete something, then confirm it's actually gone. A stateless mock can't do any of that, because it has no memory between requests — it was told to always return the same thing, and it will, forever, regardless of what you just "created."

The tell that you need a stateful mock

If the manual test plan for a feature includes the word "then" — create a thing, then list things, then update the thing, then confirm it changed — a stateless mock is going to actively work against you. You'll either give up and hardcode the "after" state as if it already happened, which means you're not actually testing the create/update flow at all, or you'll reach for a real database, which reintroduces exactly the setup cost and shared-state fragility you were trying to avoid by mocking in the first place.

A stateful mock sits in between: it behaves like a real resource collection — supporting create, read, update, and delete against an in-memory (or otherwise ephemeral) store — without requiring you to stand up an actual database, write migrations, or worry about test data colliding with someone else's.

What "stateful" actually needs to get right

A stateful mock is more than "remember the last thing I was told." To be genuinely useful for testing a real CRUD flow, it needs a few specific behaviors, because these are exactly the details that are easy to get subtly wrong and that then teach your frontend the wrong lesson:

  • POST should create with either a caller-supplied id or a generated one. Some APIs let you specify the id on create; most generate one server-side. Both are common enough that a mock needs to support "use the id I sent" as well as "generate one for me."
  • PUT and PATCH are not interchangeable, and a mock that treats them the same will hide a real bug. The conventional (and RFC-aligned) distinction: PUT replaces a resource wholesale and is typically idempotent — call it twice with the same body, get the same end state, and it's reasonable for it to create the resource if it doesn't exist yet. PATCH merges a partial update into an existing resource, and calling it against something that doesn't exist should fail rather than silently create a half-formed record. If your mock lets PATCH create resources the way PUT does, you won't discover your frontend is calling the wrong verb until it hits a real backend that enforces the difference.
  • A DELETE should actually remove the thing, such that a subsequent GET for that id returns a real 404 — not a cached "still there" response, and not a silent no-op.
  • Data needs to survive across multiple requests in a test session, but not necessarily forever — a mock's stored state existing for as long as the mock does, and no longer, is usually exactly the right lifetime: long enough to run a full manual or automated flow, without becoming a database you now have to think about maintaining.

When you still don't want statefulness

Not every route benefits from this. A read-only reference endpoint — country lists, a config blob, a pricing table — has no "then" in its usage pattern, and giving it CRUD semantics it doesn't need just adds complexity for no benefit. The right default is: static response for anything that's genuinely static, stateful only for the handful of resources your test flow actually creates, mutates, and re-reads.

In MockBase, this is a per-route toggle rather than an all-or-nothing setting for the whole mock — Stateful CRUD turns exactly the routes that need it into a real in-memory collection (with PUT-upserts-but-PATCH-requires-existing semantics matching the convention above), while everything else on the same mock can stay a simple fixed response. That mix — mostly static, selectively stateful — tends to match how real applications are actually structured far better than an all-static or all-stateful mock would.

X

Xerat02

Building MockBase.