-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Describe the bug
When using WebApplicationFactory/WebApplication in conjunction with WebApplicationFactory<T> for integration tests, the WebApplication created by WebApplicationFactory does not observe configuration changes made to the IWebHostBuilder by WebApplicationFactory<T>.
First, if the configuration is changed using ConfigureAppConfiguration(), the changes are not visible in the IConfiguration resolved by the WebApplication. This applies to adding new sources as well as clearing the existing sources. In both cases, the original configuration remains with the built application, and the changes applied for the tests by the WebApplicationFactory<T> are not visible to the application.
Calling serviceProvider.GetServices<IConfiguration>().Count() within the application returns a value of 2. It appears that both two IConfiguration implementations, one for the web host and another for WebApplication are registered with the service provider, with the WebApplication one being resolved by calls to GetService<IConfiguration>() meaning that the changes are not observed.
This can be worked around by using ConfigureServices() to clear the duplicate registration to leave the one required by the tests:
builder.ConfigureServices(services =>
{
var descriptors = services.Where(p => p.ServiceType == typeof(IConfiguration)).ToList();
services.Remove(descriptors[1]);
});Secondly, changes to the content root are not observed by the application. If UseContentRoot() is used to change the directory to serve content by WebApplicationFactory<T>, the change is not visible to the WebApplication instance, which continues to use the path associated with where the test process was launched from.
This causes static content to not be found when requested.
To Reproduce
An application that reproduces this issue is available at https://github.com/martincostello/WebApplicationFactory-Config-Issues. To reproduce the issue:
- Clone
https://github.com/martincostello/WebApplicationFactory-Config-Issues.git - Run
build.ps1
The following test failures are then observed:
- The
Can_Override_Configurationtest fails because the original value ofValueis used from configuration ("a") instead of the one overridden by the tests ("b"). - The
Can_Override_ContentRoottests fails because a 404 is returned due to the application being unable to find thetest.txtfile to serve the static content. - The
Can_Remove_Configurationtest fails because the original value ofValueis used from configuration ("a") instead of an empty value of""because all of the configuration sources were removed.
Running the application under the debugger in Visual Studio 2022 and requesting the /api or /test.txt resources in a browser show the application works as expected when run outside of the test project.
Similarly the Application_Returns_Defaults test also works correctly as no overrides are provided.
Further technical details
- .NET SDK:
6.0.100-preview.6.21324.1(I tried to use the latest preview 7 build but had issues running it in Visual Studio 2022) - Visual Studio 2022 Preview 1.1