mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-23 16:58:27 +02:00
Created Using WireMock in UnitTests (markdown)
43
Using-WireMock-in-UnitTests.md
Normal file
43
Using-WireMock-in-UnitTests.md
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
# WireMock with your favorite UnitTest framework
|
||||||
|
Obviously you can use your favourite test framework and use WireMock within your tests. In order to avoid flaky tests you should:
|
||||||
|
- let WireMock choose dynamicaly ports. It might seem common sens, avoid hard coded ports in your tests!
|
||||||
|
- clean up the request log or shutdown the server at the end of each test
|
||||||
|
|
||||||
|
Below a simple example using Nunit and NFluent test assertion library:
|
||||||
|
```csharp
|
||||||
|
[SetUp]
|
||||||
|
public void StartMockServer()
|
||||||
|
{
|
||||||
|
_server = FluentMockServer.Start();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async void Should_respond_to_request()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
_sut = new SomeComponentDoingHttpCalls();
|
||||||
|
|
||||||
|
_server
|
||||||
|
.Given(Request.Create().WithUrl("/foo").UsingGet())
|
||||||
|
.RespondWith(
|
||||||
|
Response.Create()
|
||||||
|
.WithStatusCode(200)
|
||||||
|
.WithBody(@"{ ""msg"": ""Hello world!"" }")
|
||||||
|
);
|
||||||
|
|
||||||
|
// when
|
||||||
|
var response = _sut.DoSomething();
|
||||||
|
|
||||||
|
// then
|
||||||
|
Check.That(response).IsEqualTo(EXPECTED_RESULT);
|
||||||
|
|
||||||
|
// and optionnaly
|
||||||
|
Check.That(_server.SearchLogsFor(Request.WithUrl("/error*")).IsEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TearDown]
|
||||||
|
public void ShutdownServer()
|
||||||
|
{
|
||||||
|
_server.Stop();
|
||||||
|
}
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user