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.
Return fake data
Section titled “Return fake data”A template fills the body in when the mock answers: here from the request’s path and from fake data.
-
In the project’s Mocks, select New mock.
-
Enter the Name
Get order. -
Select Regex, and enter the path
/v1/orders/[0-9]+. The method stays GET, and the mock answers any order number. -
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.randomtakes a fake-data provider and method; type{{in the editor for suggestions. -
Select Try it. The Try this mock dialog opens.
-
Set Request path to
/v1/orders/8821, and select Preview this response.Under Preview, the status is
200and 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"} -
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.
-
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.
Fail once, then succeed
Section titled “Fail once, then succeed”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.
-
Select New mock, and enter the Name
Create order. -
Choose POST, keep Path and query, and enter the path
/v1/orders. -
Under What it answers, the mock has one response, open below its tab. Enter
Try againas its Response name, set Status code to503, and the body to:{ "error": "Try again" } -
Select Response to add a second one. Enter
Placedas its Response name, set its Status code to201, and its body to:{ "id": "{{randomInt lower=1000 upper=9999}}", "status": "placed" } -
Open How a response is chosen and, beside Answer with, select A sequence. Choose
Try againfor Step 1 andPlacedfor Step 2. -
Set After the last step to Keep answering with the last.
-
Select Save mock.
Try it plays only the response you have open, so call the address to see the sequence at work:
for i in 1 2 3; do curl -s -o /dev/null -w '%{http_code}\n' -X POST https://orders-api.mockhub.dev/v1/ordersdone503201201With 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.
Remember a cancellation
Section titled “Remember a cancellation”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.
-
Open Scenarios in the project, and select New scenario.
-
Enter the Scenario name
order-8821. -
Enter
cancelledas the first of its States, and select Create scenario. Every scenario also has the stateStarted, where it begins. -
Open Mocks, select New mock, and name it
Cancel order. -
Choose POST, and enter the exact path
/v1/orders/8821/cancel. -
Keep Status code at
200, and enter the body{ "id": "8821", "status": "cancelled" }. -
Under Scenario, choose
order-8821as the Scenario,Startedin Answers only in state andcancelledin Then moves it to, and select Save mock. -
Create a second mock on the same method and path, named
Cancel order again, with Status code409and the body{ "error": "Order 8821 is already cancelled." }. -
Under Scenario, choose
order-8821, andcancelledin Answers only in state. Leave Then moves it to as it is, and select Save mock.
Call the path twice:
curl -w '\n' -X POST https://orders-api.mockhub.dev/v1/orders/8821/cancelcurl -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.
Result
Section titled “Result”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.
Limits
Section titled “Limits”- 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 resetin 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.