Skip to content

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.

  1. Open the project’s Scenarios tab.

  2. Select New scenario.

  3. Enter payment as the Scenario name.

  4. Under States, enter Pending in the first box.

  5. Select Add a state, and enter Settled.

  6. Select Create scenario.

Every scenario begins in Started, which is never listed among its states.

  1. On the project’s Mocks tab, select Create payment.

  2. Under Scenario, choose payment in Scenario.

  3. Choose Started in Answers only in state.

  4. Choose Pending in Then moves it to.

  5. Select Save mock.

  6. Attach Payment pending and Payment settled the 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.

Call the flow in order:

Terminal window
curl -i -X POST https://orders-api.mockhub.dev/payments
curl https://orders-api.mockhub.dev/payments/42
curl https://orders-api.mockhub.dev/payments/42

The 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.

  • 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 from Started. Removing the state a scenario is in from its list puts it back to Started.
  • 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 503 with 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 POST later, 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 :, and Started cannot 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.

Report a problem with this page