mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-17 23:14:23 +01:00
Updated Scenarios and States (markdown)
@@ -1,41 +1,41 @@
|
||||
# Scenarios
|
||||
# Using Scenarios
|
||||
|
||||
WireMock.Net supports state via the notion of scenarios. A scenario is essentially a state machine whose states can be arbitrarily assigned. Stub mappings can be configured to match on scenario state, such that stub A can be returned initially, then stub B once the next scenario state has been triggered.
|
||||
WireMock.Net supports _State_ via the notion of _scenarios_. A scenario is essentially a state machine whose states can be arbitrarily assigned. Stub mappings can be configured to match on scenario state, such that stub A can be returned initially, then stub B once the next scenario state has been triggered.
|
||||
|
||||
For example, suppose we’re writing a to-do list application consisting of a rich client of some kind talking to a REST service. We want to test that our UI can read the to-do list, add an item and refresh itself, showing the updated list.
|
||||
|
||||
Example test code:
|
||||
```c#
|
||||
``` c#
|
||||
// Assign
|
||||
_server = WireMockServer.Start();
|
||||
|
||||
_server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/todo/items")
|
||||
.UsingGet())
|
||||
.InScenario("To do list")
|
||||
.WillSetStateTo("TodoList State Started")
|
||||
.RespondWith(Response.Create()
|
||||
.WithBody("Buy milk"));
|
||||
.Given(Request.Create()
|
||||
.WithPath("/todo/items")
|
||||
.UsingGet())
|
||||
.InScenario("To do list")
|
||||
.WillSetStateTo("TodoList State Started")
|
||||
.RespondWith(Response.Create()
|
||||
.WithBody("Buy milk"));
|
||||
|
||||
_server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/todo/items")
|
||||
.UsingPost())
|
||||
.InScenario("To do list")
|
||||
.WhenStateIs("TodoList State Started")
|
||||
.WillSetStateTo("Cancel newspaper item added")
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(201));
|
||||
.Given(Request.Create()
|
||||
.WithPath("/todo/items")
|
||||
.UsingPost())
|
||||
.InScenario("To do list")
|
||||
.WhenStateIs("TodoList State Started")
|
||||
.WillSetStateTo("Cancel newspaper item added")
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(201));
|
||||
|
||||
_server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/todo/items")
|
||||
.UsingGet())
|
||||
.InScenario("To do list")
|
||||
.WhenStateIs("Cancel newspaper item added")
|
||||
.RespondWith(Response.Create()
|
||||
.WithBody("Buy milk;Cancel newspaper subscription"));
|
||||
.Given(Request.Create()
|
||||
.WithPath("/todo/items")
|
||||
.UsingGet())
|
||||
.InScenario("To do list")
|
||||
.WhenStateIs("Cancel newspaper item added")
|
||||
.RespondWith(Response.Create()
|
||||
.WithBody("Buy milk;Cancel newspaper subscription"));
|
||||
|
||||
// Act and Assert
|
||||
string url = "http://localhost:" + _server.Ports[0];
|
||||
@@ -65,4 +65,26 @@ The first Scenario and State definition can also be used in the JSON Admin inter
|
||||
"Body": "Buy milk"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# Stay in the same State for a number of requests
|
||||
In case you want to match a request for a certain state multiple times before moving to the next state, you can specify this. Example code:
|
||||
|
||||
In the above scenario, if you want to add more items to the ToDo list, like
|
||||
- Fixing the car
|
||||
- Cancel newspaper
|
||||
|
||||
And you want to move to the next state when these two requests are matched, set the `times` variable to `2` like this:
|
||||
|
||||
|
||||
``` c#
|
||||
_server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/todo/items")
|
||||
.UsingPost())
|
||||
.InScenario("To do list")
|
||||
.WhenStateIs("TodoList State Started")
|
||||
.WillSetStateTo("Cancel newspaper item added", 2) // <-- The number of times this match should be matched before the state will be changed to the specified one.
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(201));
|
||||
```
|
||||
Reference in New Issue
Block a user