-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Describe the bug
Compared to the use cases for IHostBuilder and IWebHostBuilder, WebApplicationFactory<T> does not set the default environment for the application under test to Development when the code path for minimal hosting is used.
| hostBuilder?.UseEnvironment(Environments.Development); |
| return builder.UseEnvironment(Environments.Development); |
aspnetcore/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs
Lines 152 to 178 in dbf84ea
| var hostBuilder = CreateHostBuilder(); | |
| if (hostBuilder is not null) | |
| { | |
| ConfigureHostBuilder(hostBuilder); | |
| return; | |
| } | |
| var builder = CreateWebHostBuilder(); | |
| if (builder is null) | |
| { | |
| var deferredHostBuilder = new DeferredHostBuilder(); | |
| // This helper call does the hard work to determine if we can fallback to diagnostic source events to get the host instance | |
| var factory = HostFactoryResolver.ResolveHostFactory( | |
| typeof(TEntryPoint).Assembly, | |
| stopApplication: false, | |
| configureHostBuilder: deferredHostBuilder.ConfigureHostBuilder, | |
| entrypointCompleted: deferredHostBuilder.EntryPointCompleted); | |
| if (factory is not null) | |
| { | |
| // If we have a valid factory it means the specified entry point's assembly can potentially resolve the IHost | |
| // so we set the factory on the DeferredHostBuilder so we can invoke it on the call to IHostBuilder.Build. | |
| deferredHostBuilder.SetHostFactory(factory); | |
| ConfigureHostBuilder(deferredHostBuilder); | |
| return; | |
| } |
Adding the missing line itself does not seem to work though, possibly related to #33876 - I've got some tests from debugging this locally I'll push up into a PR shortly see #33890.
To Reproduce
Inspect the IHostEnvironment.EnvironmentName property in, for example, the HelloWorld test.
aspnetcore/src/Mvc/test/Mvc.FunctionalTests/SimpleWithWebApplicationBuilderTests.cs
Lines 21 to 32 in 9a451a3
| [Fact] | |
| public async Task HelloWorld() | |
| { | |
| // Arrange | |
| var expected = "Hello World"; | |
| // Act | |
| var content = await Client.GetStringAsync("http://localhost/"); | |
| // Assert | |
| Assert.Equal(expected, content); | |
| } |
Further technical details
- ASP.NET Core
6.0.0-preview.6.21323.4 - .NET SDK
6.0.100-preview.6.21324.1 - Visual Studio 2022 17.0.0 Preview 1.1