-
-
Notifications
You must be signed in to change notification settings - Fork 212
Using WireMock in UnitTests
Stef Heyenrath edited this page Apr 24, 2020
·
11 revisions
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:
[SetUp]
public void StartMockServer()
{
_server = WireMockServer.Start();
}
[Test]
public async void Should_respond_to_request()
{
// Assign
_sut = new SomeComponentDoingHttpCalls();
_server
.Given(Request.Create().WithPath("/foo").UsingGet())
.RespondWith(
Response.Create()
.WithStatusCode(200)
.WithBody(@"{ ""msg"": ""Hello world!"" }")
);
// Act
var response = _sut.DoSomething();
// Assert
Check.That(response).IsEqualTo(EXPECTED_RESULT);
}
[TearDown]
public void ShutdownServer()
{
_server.Stop();
}
For some more examples: see https://github.com/bredah/csharp-wiremock
- Home
- What is WireMock.Net
- WireMock.Org
- References
- Settings
- Admin REST API
- Proxying
- Stubbing
- Webhook
- Request Matching
- Response Templating
- Unit Testing
- Using WireMock
- Advanced
- Errors