C
C#10mo ago
fasadin

config from test it's not used

I am running intergration test. I've implemented it by using WebApplicationFactory here it's how I am getting client
public static HttpClient CreateClient()
{
var projectDir = Directory.GetCurrentDirectory();
var configPath = Path.Combine(projectDir, "appsettings.test.json");
var factory = new WebApplicationFactory<Program>();
factory = factory.WithWebHostBuilder(
builder =>
{
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "test");
builder.ConfigureAppConfiguration((_, conf) => { conf.AddJsonFile(configPath); });
builder.UseEnvironment("test");
}
);
var client = factory.CreateClient();
return client;
}
public static HttpClient CreateClient()
{
var projectDir = Directory.GetCurrentDirectory();
var configPath = Path.Combine(projectDir, "appsettings.test.json");
var factory = new WebApplicationFactory<Program>();
factory = factory.WithWebHostBuilder(
builder =>
{
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "test");
builder.ConfigureAppConfiguration((_, conf) => { conf.AddJsonFile(configPath); });
builder.UseEnvironment("test");
}
);
var client = factory.CreateClient();
return client;
}
in Program.cs when I will check what's inside configuration builder. I can see that appsettings.test.json it's one of it's sources. Yet, it is using appsettings.json from application and not from test. Here is code How I retrieve settings

var configurationBuilder = builder
.Configuration
.AddJsonFile($"appsettings.{environment}.json", true, true)
.AddJsonFile("appsettings.json", false, true)
.AddEnvironmentVariables();

var configuration = configurationBuilder.Build();
var settings = configuration.GetSection("Settings").Get<Settings>();

var configurationBuilder = builder
.Configuration
.AddJsonFile($"appsettings.{environment}.json", true, true)
.AddJsonFile("appsettings.json", false, true)
.AddEnvironmentVariables();

var configuration = configurationBuilder.Build();
var settings = configuration.GetSection("Settings").Get<Settings>();
file it's copied and it's in debug folder. Environment it's set to test. I realize, webapplication factory has "double" instructions (setting environment, or adding json file to builder which is not needed). It's just current state of code after many attempts of fixing it
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server