Model a stateful flow
Before you begin
- Role
- Editor or higher.
- You need
- The mocks of the flow; see Create and edit a mock
A scenario is a named set of states in a project. Each mock in a flow says in which state it answers and which state it moves the scenario to, so one request gets different answers as the flow goes on. This page models a payment: POST /payments starts it, and GET /payments/42 then answers pending once and settled from then on.
| Mock | Request | Answers only in state | Then moves it to | Answers |
|---|---|---|---|---|
Create payment |
POST /payments |
Started |
Pending |
202 and {"id": 42, "status": "pending"} |
Payment pending |
GET /payments/42 |
Pending |
Settled |
{"id": 42, "status": "pending"} |
Payment settled |
GET /payments/42 |
Settled |
Leave unchanged | {"id": 42, "status": "settled"} |
Two mocks can share a method and path when they answer in different states: only one of them matches at a time.
Create the scenario
Section titled “Create the scenario”-
Open the project’s Scenarios tab.
-
Select New scenario.
-
Enter
paymentas the Scenario name. -
Under States, enter
Pendingin the first box. -
Select Add a state, and enter
Settled. -
Select Create scenario.
Every scenario begins in Started, which is never listed among its states.
Attach the mocks
Section titled “Attach the mocks”-
On the project’s Mocks tab, select
Create payment. -
Under Scenario, choose
paymentin Scenario. -
Choose
Startedin Answers only in state. -
Choose
Pendingin Then moves it to. -
Select Save mock.
-
Attach
Payment pendingandPayment settledthe same way, with the states in the table.
A mock left at Any state answers whatever state the scenario is in, and one left at Leave unchanged does not move it. An attachment needs at least one of the two.
Result
Section titled “Result”Call the flow in order:
curl -i -X POST https://orders-api.mockhub.dev/paymentscurl https://orders-api.mockhub.dev/payments/42curl https://orders-api.mockhub.dev/payments/42The POST gets 202, the first GET answers pending, and every GET after it answers settled. Before the POST, no GET mock matches: in Mock only, GET /payments/42 gets a 404 No mock matched.
The Scenarios tab shows which state payment is in, and puts it back to Started for the next run; see Inspect and reset scenario state.
Limits
Section titled “Limits”- A scenario has one state for the whole project. Every caller, test run and teammate moves the same state, so reset it before a run.
- MockHub keeps the state until the scenario goes seven days unused; then it goes back to
Started. Renaming a scenario keeps its state; deleting it and creating one with the same name starts fromStarted. Removing the state a scenario is in from its list puts it back toStarted. - A move stands once a mock has made it, even when the answer never reaches the caller, as after a dropped connection, or when a chaos profile or a template error replaces the answer.
- When MockHub cannot read or save the state, a mock of the flow answers
503with Scenario unavailable, and rarely, when other moves keep landing in between, Scenario conflict. - States are a fixed list you define, and a scenario keeps nothing from a request. To return the body of a
POSTlater, use the data store. - A mock with a response sequence cannot also join a scenario. For one mock that answers differently on each call, a sequence is the simpler tool.
- Try it starts the scenario in the state you choose, the project’s current one unless you pick another, and moves only its own copy: the project’s scenario stays where it is. See Test a mock before saving.
- A scenario with mocks attached cannot be deleted; detach them first. A state a mock still uses cannot be removed from the list; MockHub names those mocks under States.
- A scenario name cannot contain
:, andStartedcannot be declared as a state. Names are unique within a project. - A scenario has at most 50 states besides
Started, each at most 255 characters long. - The plan sets how many scenarios a project can have; the limits page lists the number. Scenarios compiled from response sequences do not count.
- Scenarios do not run in the Record everything gateway mode; see Gateway modes.