Xunit读取应用程序AppSetting文件不是在.NET Core中

发布于 2025-01-27 01:13:24 字数 1972 浏览 2 评论 0 原文

您可以看到,我有一个带有此配置的测试解决方案:

 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 < q <上放置一个断点。 /code>变量,其值不是 appsettingtest 的内容,而是我的应用程序中的 appSetting 的内容。为什么?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日 2025-02-03 01:13:24

只需设置以下设置:

并将配置选项设置为

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

Just set this :

enter image description here

and set the config options to

    config.AddJsonFile("appSettings.json", false,false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文