How to add middleware to WebApplicationFactory<TEntryPoint> ? #53999
Replies: 3 comments 1 reply
-
HI :) Yes, it is possible to add middleware to the application being tested. However, the issue you’re encountering is likely due to the fact that the middleware you added does not call the next middleware in the pipeline. In ConfigureWebHost :
|
Beta Was this translation helpful? Give feedback.
-
This code returns a 404 - Not Found. public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.Configure(app =>
{
app.Use(async (ctx, next) => { await next(); });
});
base.ConfigureWebHost(builder);
}
} This code returns a 200 - OK public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
}
} It is definitely -NOT- the middleware. |
Beta Was this translation helpful? Give feedback.
-
And for full disclosure - doing nothing in that action also causes a 404 - Not Found
|
Beta Was this translation helpful? Give feedback.
-
Create a simple web API application project and unit tests:
Change
Program.cs
to make theProgram
class andWeatherForecast
record public:Change
UnitTest1.cs
to be:Run the
WebApiTests.UnitTest1.Test1Async
test and the test succeeds.Change
UnitTest1.cs
to be:Now the test fails with:
Is it not possible to add middleware to the application being tested?
Beta Was this translation helpful? Give feedback.
All reactions