strange behavior with the WebApplicationFactory

23 Views Asked by At

I wrote a CustomWebApplication Factory in which I provide a user and the environment as parameters to set the user context. In the first case one of the parameter is in small letters in the other in capital letters.

--environment --ENVIRONMENT

Tester1

public class CustomWebApplicationFactoryUserTester1<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
    private const string ParameterUser = "user";

    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        // key, value
        builder.UseSetting(ParameterUser, TestConstants.UserId_Tester1);

        // Works for: Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") in Program.cs
        Environment.SetEnvironmentVariable(Environments.EnvironmentVariableCurrentEnvironment, Environments.Local);

        // Works for: builder.Environment.EnvironmentName in Program.cs
        builder.UseEnvironment(Environments.Local);
    }
}

Integration Test

// Arrange
await using CustomWebApplicationFactoryUserTester1<Program> application = new();
using HttpClient client = application.CreateClient();

Tester2

public class CustomWebApplicationFactoryUserTester2<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
    private const string ParameterUser = "user";

    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        // key, value
        builder.UseSetting(ParameterUser, TestConstants.UserId_Tester2);

        // Works for: Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") in Program.cs
        Environment.SetEnvironmentVariable(Environments.EnvironmentVariableCurrentEnvironment, Environments.Local);

        // Works for: builder.Environment.EnvironmentName in Program.cs
        builder.UseEnvironment(Environments.Local);
    }
}

Integration Test

        // Arrange
        await using CustomWebApplicationFactoryUserTester2<Program> application = new();
        using HttpClient client = application.CreateClient();

In the first case the args of the Programm are "--environment and in the second case are "--ENVIRONMENT".

I don't understand where the strange behavior comes from. I use constants and the code is identical.

I use the latest .Net Core dependencies.

0

There are 0 best solutions below