Search code examples
asp.net-coreconfigurationxunitappsettings

xunit read application appsetting file not its own in .net core


I have a test solution with this configuration as you can see :

 public class TestHostBuilder : RavenTestDriver, IDisposable
    {

    public IHost host = null;
    public IConfiguration Configuration;
    public IDocumentStore documentStore = null;
    public TestHostBuilder()
    {
        ConfigureServer(new TestServerOptions() {FrameworkVersion = null,});
        documentStore = GetDocumentStore();
        Environment.SetEnvironmentVariable("TEST_ENV", "on");
        var hostBuilder = easy.api.Program.CreateHostBuilder(new string[0])
    .ConfigureWebHost(webHostBuilder =>
    {
        webHostBuilder.UseTestServer();
    }).ConfigureAppConfiguration(config =>
    {
        //config.Configure<domain.Environments.RavenOptions>(configuration.GetSection(domain.Environments.RavenOptions.DefaultSectionName));

        config.AddJsonFile("appSettingTest.json", optional: true);
        Configuration=config.Build();
    })
   .ConfigureServices(services =>
   {
      
       services.Configure<IOptions<domain.Environments.RavenOptions>>(options => Configuration.GetSection("RavenOptions").Bind(options));
       var q = services.BuildServiceProvider().GetRequiredService<IOptions<domain.Environments.RavenOptions>>().Value;

       services.AddScoped<ICurrentUserService, InitRequest>();
       services.AddScoped<ICacheStorage>(provider =>
       {
           return new Mock<ICacheStorage>().Object;
       });
       services.AddRavenDbAsyncSession(GetDocumentStore(new GetDocumentStoreOptions()));
       services.AddTransient<IAsyncDocumentSession>((c) =>
        {
            return documentStore.OpenAsyncSession();
        });

   });

        host = hostBuilder.Start();

    }
}

As you can see I added the appSettingTest file into my test project but when I run the test and put a break point on q variable, the value of that is not the content of appSettingTest,it is the content of appSetting in my application .why ?


Solution

  • Just set this :

    enter image description here

    and set the config options to

        config.AddJsonFile("appSettings.json", false,false);