-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Ammending the request pipeline with WebApplicationFactory #3254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You have to subclass |
Can you provide an example @ardalis as the following doesnt work. public class MiddlewareTestFixture : WebApplicationFactory<Startup>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
// this line prevents Startup.Configure from firing
builder.Configure(app =>
{
app.Map("/error", applicationBuilder => { applicationBuilder.Run(_ => throw new Exception()); });
});
base.ConfigureWebHost(builder);
}
}
public class Test : IClassFixture<MiddlewareTestFixture>
{
private readonly HttpClient _client;
public Test(MiddlewareTestFixture fixture)
{
_client = fixture.CreateClient();
}
[Fact]
public async Task Me()
{
var result = await _client.GetAsync("/error");
result.StatusCode.ShouldBe(HttpStatusCode.InternalServerError);
}
} As soon as I use |
You're correct that any call to builder.Configure will override Startup.Configure. I think you might want to try a different approach, such as specifying an environment (e.g. Testing) and in Startup checking that environment and modifying the request pipeline accordingly. Alternately, just create a controller action that you know throws an exception and hit that in your test. |
The controller action or the custom environment are the ways to go. Getting in the middle of the pipeline through the use of services is not possible. |
Im trying to create a integration test for my Exception handling middleware How do you add to the existing
ApplicationBuilder
with WebApplicationFactory?Im using
WebApplicationFactory<T>
with myStartup.cs
and I want to add an additional middleware to the request pipeline so I can mimic a error response.The example below completely overrides my Startup which is not the desired result. I noticed builder has the following extension methods
ConfigureTestServices()
andConfigureTestContainer()
but there doesnt appear to be an equivilant to configure the request pipeline thats under test.Perhaps I could use a
IStartupFilter
which throws at the end of the request pipeline. But im not sure how I could get that to invoke after my middleware.This seems to do it but its alot of boiler plate. I'm hoping im missing something obvious.
The text was updated successfully, but these errors were encountered: