xunit 读取应用程序 appsetting 文件不是它自己在 .net 核心中
xunit read application appsetting file not its own in .net core
如您所见,我有一个使用此配置的测试解决方案:
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();
}
}
如您所见,我将 appSettingTest
文件添加到我的测试项目中,但是当我 运行 测试并在 q
变量上放置一个断点时,它的值是不是 appSettingTest
的内容,它是我应用程序中 appSetting
的内容。为什么?
只需设置这个:
并将配置选项设置为
config.AddJsonFile("appSettings.json", false,false);
如您所见,我有一个使用此配置的测试解决方案:
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();
}
}
如您所见,我将 appSettingTest
文件添加到我的测试项目中,但是当我 运行 测试并在 q
变量上放置一个断点时,它的值是不是 appSettingTest
的内容,它是我应用程序中 appSetting
的内容。为什么?
只需设置这个:
并将配置选项设置为
config.AddJsonFile("appSettings.json", false,false);