mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-25 01:38:27 +02:00
Created Scenarios and Statefull behaviour (markdown)
50
Scenarios-and-Statefull-behaviour.md
Normal file
50
Scenarios-and-Statefull-behaviour.md
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
# 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.
|
||||||
|
|
||||||
|
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#
|
||||||
|
// Assign
|
||||||
|
_server = FluentMockServer.Start();
|
||||||
|
|
||||||
|
_server
|
||||||
|
.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));
|
||||||
|
|
||||||
|
_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"));
|
||||||
|
|
||||||
|
// Act and Assert
|
||||||
|
string url = "http://localhost:" + _server.Ports[0];
|
||||||
|
string getResponse1 = await new HttpClient().GetStringAsync(url + "/todo/items");
|
||||||
|
Check.That(getResponse1).Equals("Buy milk");
|
||||||
|
|
||||||
|
var postResponse = await new HttpClient().PostAsync(url + "/todo/items", new StringContent("Cancel newspaper subscription"));
|
||||||
|
Check.That(postResponse.StatusCode).Equals(HttpStatusCode.Created);
|
||||||
|
|
||||||
|
string getResponse2 = await new HttpClient().GetStringAsync(url + "/todo/items");
|
||||||
|
Check.That(getResponse2).Equals("Buy milk;Cancel newspaper subscription");
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user