Skip to content

Make a mock realistic

Before you begin

Role
Editor or higher.
You need
  • The project from Your first mock in five minutes, or any project you can edit

A fixed body is enough to start, but a client under test needs more: different data on each call, failures to retry, and answers that depend on what happened before. This page builds all three on one orders API.

Response templating is on for every new project, so the expressions below work without changing a setting. The address is written as https://orders-api.mockhub.dev; use your project’s.

A template fills the body in when the mock answers: here from the request’s path and from fake data.

  1. In the project’s Mocks, select New mock.

  2. Enter the Name Get order.

  3. Select Regex, and enter the path /v1/orders/[0-9]+. The method stays GET, and the mock answers any order number.

  4. Replace the {} under Response body with:

    {
    "id": "{{request.pathSegments.[2]}}",
    "status": "{{pickRandom 'placed' 'paid' 'shipped'}}",
    "customer": {
    "name": "{{random 'Name.fullName'}}",
    "email": "{{random 'Internet.safeEmailAddress'}}"
    },
    "item": "{{random 'Commerce.productName'}}",
    "quantity": {{randomInt lower=1 upper=5}},
    "placedOn": "{{now format='yyyy-MM-dd'}}"
    }

    request.pathSegments.[2] is the third part of the path, the order number, because parts count from 0. random takes a fake-data provider and method; type {{ in the editor for suggestions.

  5. Select Try it. The Try this mock dialog opens.

  6. Set Request path to /v1/orders/8821, and select Preview this response.

    Under Preview, the status is 200 and the body is filled in, such as:

    {
    "id": "8821",
    "status": "paid",
    "customer": { "name": "Ada Lovelace", "email": "ada@example.com" },
    "item": "Ergonomic Steel Chair",
    "quantity": 3,
    "placedOn": "2026-09-30"
    }
  7. Select Preview this response again. The id stays 8821, and the other values are drawn again, so most of them change.

    If an expression is wrong, the dialog lists the problem instead; see Test a mock before saving.

  8. Close the dialog, and select Save mock.

Try it runs the mock as it stands, without saving it, so it is the quickest way to check a template.

A response sequence answers each call with the next step in its list, and each step names one of the mock’s responses. Here the first call to create an order fails, and every call after it succeeds, which is what a retry needs.

  1. Select New mock, and enter the Name Create order.

  2. Choose POST, keep Path and query, and enter the path /v1/orders.

  3. Under What it answers, the mock has one response, open below its tab. Enter Try again as its Response name, set Status code to 503, and the body to:

    { "error": "Try again" }
  4. Select Response to add a second one. Enter Placed as its Response name, set its Status code to 201, and its body to:

    { "id": "{{randomInt lower=1000 upper=9999}}", "status": "placed" }
  5. Open How a response is chosen and, beside Answer with, select A sequence. Choose Try again for Step 1 and Placed for Step 2.

  6. Set After the last step to Keep answering with the last.

  7. Select Save mock.

Try it plays only the response you have open, so call the address to see the sequence at work:

Terminal window
for i in 1 2 3; do
curl -s -o /dev/null -w '%{http_code}\n' -X POST https://orders-api.mockhub.dev/v1/orders
done
503
201
201

With Start over from the first, the third call would fail again. A mock can also keep several responses without a sequence, one of them the default, and let a request ask for another; see Choose the response that answers.

A scenario is a set of states the project remembers between calls. Two mocks on the same path can then answer differently: the first cancellation succeeds, and a second one is refused.

  1. Open Scenarios in the project, and select New scenario.

  2. Enter the Scenario name order-8821.

  3. Enter cancelled as the first of its States, and select Create scenario. Every scenario also has the state Started, where it begins.

  4. Open Mocks, select New mock, and name it Cancel order.

  5. Choose POST, and enter the exact path /v1/orders/8821/cancel.

  6. Keep Status code at 200, and enter the body { "id": "8821", "status": "cancelled" }.

  7. Under Scenario, choose order-8821 as the Scenario, Started in Answers only in state and cancelled in Then moves it to, and select Save mock.

  8. Create a second mock on the same method and path, named Cancel order again, with Status code 409 and the body { "error": "Order 8821 is already cancelled." }.

  9. Under Scenario, choose order-8821, and cancelled in Answers only in state. Leave Then moves it to as it is, and select Save mock.

Call the path twice:

Terminal window
curl -w '\n' -X POST https://orders-api.mockhub.dev/v1/orders/8821/cancel
curl -w '\n' -X POST https://orders-api.mockhub.dev/v1/orders/8821/cancel
{ "id": "8821", "status": "cancelled" }
{ "error": "Order 8821 is already cancelled." }

To start over, open Scenarios and select Reset on order-8821. A message says it is back in Started, and the next cancellation succeeds again.

The orders API now returns a different order on every read, fails the first attempt to create one, and refuses to cancel the same order twice, which covers the paths most clients forget to test.

  • Try it tests one mock and one response. It plays only the open response of a sequence, so check sequences by calling the address.
  • Sequence and scenario positions are shared by everyone calling the project, and MockHub keeps them until they go seven days unused. Reset all on Scenarios, or mockhub scenarios reset in a pipeline, puts both back before a test run; see Inspect and reset scenario state.
  • A mock with a response sequence cannot also take part in a scenario, and takes no request override.
  • A scenario moves between the states you declared, not between records. To keep what a request sent, use the data store.

Report a problem with this page