mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 13:00:33 +01:00
Page:
Using WireMock in UnitTests
Pages
Admin API Reference
Compatibility WireMock.org
Conflict on Microsoft.CodeAnalysis.CSharp
Cors
Could not load file or assembly RestEase
Development Information
Faults
FluentAssertions
Home
KestrelServerOptions
Mapping
MimeKit and MimeKitLite
MyGet preview versions
Pact
Proxying
References
RegexExtended
Request Matcher FormUrlEncodedMatcher
Request Matchers
Request Matching CSharpCode
Request Matching GraphQLMatcher
Request Matching JsonMatcher
Request Matching JsonPartialMatcher
Request Matching JsonPartialWildcardMatcher
Request Matching JsonPathMatcher
Request Matching MimePartMatcher
Request Matching ProtoBuf
Request Matching Tips
Request Matching
Response Templating
Scenarios and States
Settings
Stubbing
Using HTTPS (SSL)
Using WireMock in UnitTests
Using WireMock.Net.Aspire
Using WireMock.Net.Testcontainers
Webhook
What Is WireMock.Net
WireMock as a (Azure) Web App
WireMock as a Windows Service
WireMock as a standalone process
WireMock as dotnet tool
WireMock commandline parameters
WireMock.Org
Xamarin Could not load file or assembly
Clone
11
Using WireMock in UnitTests
Stef Heyenrath edited this page 2021-02-01 17:19:32 +01:00
Table of Contents
WireMock with your favorite UnitTest framework
Obviously you can use your favorite test framework and use WireMock.Net within your tests. In order to avoid flaky tests you should:
- let WireMock.Net choose ports dynamically. Avoid hard coded ports in your tests. This can cause issues when running these unit-tests on a build-server, there is not 100% guarantee that this port will be free on the OS.
- 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 Task Should_respond_to_request()
{
// Arrange (start WireMock.Net server)
_server
.Given(Request.Create().WithPath("/foo").UsingGet())
.RespondWith(
Response.Create()
.WithStatusCode(200)
.WithBody(@"{ ""msg"": ""Hello world!"" }")
);
// Act (use a HttpClient which connects to the URL where WireMock.Net is running)
var response = await new HttpClient().GetAsync($"{_server.Urls[0]}/foo");
// Assert
Check.That(response).IsEqualTo(EXPECTED_RESULT);
}
[TearDown]
public void ShutdownServer()
{
_server.Stop();
}
For some more examples: see https://github.com/bredah/csharp-wiremock
Pages
- 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