Middleware is often tested in isolation with Test Server. It allows you to instantiate an app pipeline containing only the components that you simply got to test.
Advantages of Middleware
- Requests are sent in-memory instead of being serialized over the network.
- Exceptions within the middleware can flow directly back to the calling test.
- It's possible to customize server data structures, like HttpContext, directly within the test.
Middleware’s offer a more powerful and flexible solution and for that, they need to be tested! Thankfully there's how to try to that so let's first see what we are getting to test then just get in there to Example:
You can need to Install Microsoft.AspNetCore.Testhst
First of All, you can need to Configure the processing pipeline to use the middleware for the test. part of program configure Processing pipeline is shown below.
[Fact]
public async Task MiddlewareTest_ReturnsNotFoundForRequest()
{
using varhst = await new hstBuilder().ConfigureWebhst(webBuilder =>
{
webBuilder.UseTestServer().ConfigureServices(services =>
{
services.AddMyServices();
})
.Configure(app =>
{
app.UseMiddleware();
});
})
.StartAsync();
}
After that you need to send requests with the HttpClient sample code is below.
[Fact]
public async Task MiddlewareTest_ReturnsNotFoundForRequest()
{
using varhst = await new hstBuilder().ConfigureWebhst(webBuilder =>
{
webBuilder.UseTestServer().ConfigureServices(services =>
{
services.AddMyServices();
})
.Configure(app =>
{
app.UseMiddleware();
});
})
.StartAsync();
var response = await hst.GetTestClient().GetAsync("/");
}
Assert the result and make an assertion of the opposite of the result that you expect. An embryonic run with a false positive assertion confirms that the test fails. when the middleware is performing Truly. Start test and confirm that the test is failed.
In this example, the middleware returns a 404-status code which is NOT FOUND code when the root endpoint is requested. Make the first one test run with Assert.NotEqual (); which should be fail.
[Fact]
public async Task MiddlewareTest_ReturnsNotFoundForRequest()
{
using varhst = await new hstBuilder().ConfigureWebhst(webBuilder =>
{
webBuilder.UseTestServer().ConfigureServices(services =>
{
services.AddMyServices();
})
.Configure(app =>
{
app.UseMiddleware();
});
})
.StartAsync();
var response = await hst.GetTestClient().GetAsync("/");
Assert.NotEqual(HttpStatusCode.NotFound, response.StatusCode);
}
Change the assertion for test the middleware under normal operating ambiance. The final test uses Assert.Equal( );. Run the test a second time to confirm that it was successful.
[Fact]
public async Task MiddlewareTest_ReturnsNotFoundForRequest()
{
using varhst = await new hstBuilder()
.ConfigureWebhst(webBuilder =>
{
webBuilder.UseTestServer().ConfigureServices(services =>
{
services.AddMyServices();
})
.Configure(app =>
{
app.UseMiddleware();
});
})
.StartAsync();
var response = await hst.GetTestClient().GetAsync("/");
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}