Blog

How to test an empty cart and other rare app states

The test plan has “Empty cart”, “Out of stock”, “Delivery time taken”. You can’t get them on staging: automated tests keep the test account’s cart full, products never run out, and the time is always free. Release after release, these rows stay “not tested”, and these are exactly the screens users see. Mock cases in Microkoi Bench turn on the state you need with a single click, while the rest of the app keeps working with staging.

Why staging can’t give you this

Staging data is shared. To empty the cart, you clear it, and an hour later an automated test fills it again. For a delivery time to be taken, someone has to take it. Asking a backend developer to prepare data for every check means days of waiting.

And this is exactly where bugs live. The “Checkout” button is enabled with an empty cart and leads to a blank screen. A product is out of stock, but the cart still shows the old price. An empty list shows an endless loading indicator instead of a hint.

Record the usual path

Route the app through the Bench proxy and go through the purchase path: catalog, product page, cart, checkout. The “Endpoints” tree in the “Traffic” section sorts the requests by host and path.

Hover over the api.example.com/v1 branch and click “Create mocks for all requests in group”. Bench creates a mock for each method and path combination with the last recorded response. Mocks are created turned off, so the app keeps working with staging.

A case is a state on top of mocks

The recorded mocks are the “Base” responses. A case is a named set that overrides the status, headers and body of some of them. One case is active, or none. A slash in the name groups cases: “Cart/Empty” and “Cart/Out of stock” go into the “Cart” group.

Build the “Cart/Empty” case

  1. In the “Mocks” section, click the checkmark button above the tree and check the cart mock GET /v1/cart.
  2. Click “Add to case…”, enter the name “Cart/Empty” and click “Create”.
  3. Pick the case in the context picker at the top of the section and open the cart mock. The override editor opens.
  4. Replace the body with an empty cart and click “Save”. The base mock doesn’t change.
The “Cart/Empty” case: GET /v1/cart body
{
  "items": [],
  "itemsCount": 0,
  "total": 0,
  "currency": "USD",
  "recommendationsUrl": "https://api.example.com/v1/recommendations"
}

If the body is a JSON object, there is a “Top-level fields” block under it. You can remove a field with the cross or add one without editing the text. That’s how you check whether the screen survives a response without recommendationsUrl.

Turn the state on with one click

An open case doesn’t answer yet. The active one does: to the right of the context picker it says “Active: …” or “No active case”. An open case has a “Make active” button there, and the active one has “Deactivate”. The proxy answers the new way right away, without restarting the app.

The base mocks can stay turned off. Requests are still matched against them, and an enabled override in the active case answers even for a mock that is off. Without a case, the app works with staging; with a case, it gets the state you need, and all other requests still go to staging.

Screen stateWhat to turn on
Empty cartThe “Cart/Empty” case
Out of stockThe “Cart/Out of stock” case
Delivery time takenThe “Checkout/Delivery slot taken” case
Regular work with staging“Deactivate”

Checking the empty cart takes a minute: “Make active”, refresh the cart screen, look at the hint and the “Checkout” button, “Deactivate”. The test plan row gets checked in every release.

A server refusal is a state too

A taken delivery time isn’t a failure but a business logic response. The “Checkout/Delivery slot taken” case overrides POST /v1/orders with a 409 status and an error body. Take the body format from the API description or from a recorded response; don’t make it up.

The “Checkout/Delivery slot taken” case: 409 response
{
  "error": "delivery_slot_taken",
  "message": "This delivery time is already taken. Choose another one",
  "details": []
}

Server failures and slow responses are tested the same way.

The state in a bug report

A case response is recorded in traffic as a regular exchange. Its first column shows the blue mock cube, and the tooltip names the case and the mock it overrides. In the bug report, it’s enough to write: “case Cart/Empty”. The developer turns on the same case and sees the same thing.

That’s why cases and mocks are stored as files in the workspace folder and go to git. Which case is active doesn’t go to git: a teammate who gets the workspace won’t suddenly see an empty cart.

Good to know up front

  • Mocks and cases answer only requests through a running proxy, and only for hosts recorded in the workspace.
  • The response is static. Retrying checkout after an error is tested by deactivating the case.
  • There are no header conditions. A mock is picked by method, host, path and conditions on query parameters and body fields.
  • A case doesn’t change the delay: the base mock sets it.
  • The same cases turn on without a window, with the bench-mocks program, so automated tests can check these states.

Which rows of your test plan have stayed “not tested” for years? Build cases for them in the beta and write to beta@microkoi.com about what worked and where cases fell short. We read every email.

Try it on your own project

The beta is free and needs no sign-up. Download it, pick your project folder and start the proxy — your first requests will show up within minutes.

Version 0.9.6 · macOS, Windows and Linux · no sign-up