State management
Most of the backend services tend to have some state, which changes as user interact with it. So to be able to simulate this, mock JSON response introduces scenario-based mapping.
How to adjust your stub to make it stateful.
Just add scenario and state attributes to the request object in the stub.
scenario (required): this attribute helps to classify the group of stubs together for state management.
state (required): attribute defines the state of the stub.
targetState (optional): attribute that defines the next state of the stub
when the program is initialized, the state of a scenario will always be 'init'.
In State management, MJR will divide the scenario further into stages. During a stage, the response belonging to that stage is mapped. Then how would the state progress? On request like POST, PUT, DELETE, the stage will be changed (with the value of the matched stub's state).
For example:
In the above example, all the 3 stubs are bind via "shopping list" scenario. When a first GET request is called, MJR will scan for the match request and get 2 requests, but selects pre_list request, since the state value is 'init', which indicates that the stage is initial. When POST request is received it's processed and the stage is set to the targetState of that selected stub. In this case, "state_2". Now when GET request, post_list request is selected as presetStage of the stub is "state_2".
To reset the scenario, one can POST a request to http://localhost:3000/__admin/:scenario_name/reset, setting "add" as scenarion_name will reset all the scenarios.
Last updated
Was this helpful?