来自集成测试的ASP.NET Core UseSetting [英] ASP.NET Core UseSetting from integration test

查看:195
本文介绍了来自集成测试的ASP.NET Core UseSetting的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集成测试项目,该项目在测试类中使用.UseSetting(),如下所示:

I have an integration tests project that uses .UseSetting() in the test class, as follows:

public AccessTokenRetrieval() : base(nameof(AccessTokenRetrieval))
{
    var connectionString = GetConnectionString();
    var dbSettings = new DbSettings(connectionString);
    _userGroupRepository = new UserGroupRepository(dbSettings);
    _roleRepository = new RoleRepository(dbSettings);
    _userRepository = new UserRepository(dbSettings);

    _server = new TestServer(new WebHostBuilder()
        .UseStartup<Startup>()
        .UseEnvironment("IntegrationTest")
        .UseSetting("IntegrationTestConnString", dbSettings.IdentityServerConnectionString));
    _handler = _server.CreateHandler();
    _client = _server.CreateClient();
}

我现在想在我实际项目的Startup.cs中检索该设置.我尝试使用:

I would now like to retrieve that setting in the Startup.cs of my actual project. I attempted to do so using:

public void ConfigureIntegrationTestServices(IServiceCollection services)
{
    var connectionString = Configuration.GetValue<string>("IntegrationTestConnString");

    BuildIdentityServerTests(services, connectionString);
    AddCoreServices(services, connectionString);
}

但是这似乎返回null.

but that seems to return null.

检索此设置的正确方法是什么?

What is the proper way to retrieve this setting?

推荐答案

您可以将IConfigurationRoot注入到可以在测试项目中使用本地配置文件的位置.您将丢失默认的特定于环境的配置替代,这是新项目模板中的默认替代,但是我个人无论如何都不会将其用于配置管理(建议使用非承诺的appsetting.local.json,这对于开发人员而言是唯一的)并连接到CI服务器.

You could just inject the IConfigurationRoot where you could just use a local config file in the test project. You'd lose the default environment-specific config overrides that are the default in the new project template, but I personally don't use those for configuration management anyway (preferring non-committed appsetting.local.json instead which can be unique to developers and to the CI server).

var configuration = new ConfigurationBuilder()
    .SetBasePath(AppContext.BaseDirectory)
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: true)
    .AddInMemoryCollection(new Dictionary<string, string>
    {
        {"myConfig:setting1", "value1"},
        {"myConfig:setting2", "value2"}
    })
    .Build();

var server = new TestServer(new WebHostBuilder()
    .UseUrls("http://localhost:5001")
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseStartup<Startup>()
    .ConfigureServices(services => services.AddSingleton(configuration)));

对于启动:

public class Startup
{
    public Startup(IConfigurationRoot configuration)
    {
        this.Configuration = configuration;
    }

    ...
}

修改: 尽管这确实可行,但似乎也无法在运行时识别配置文件更改,从而中断了IOptionsSnapshot配置注入.我将离开这个答案,但也将继续挖掘更好的方法,而无需注入自定义服务来仅支持测试/夹具特定的配置替代.

While this does work, it also seems to break IOptionsSnapshot configuration injections from recognizing config file changes at run-time. I'm going to leave this answer, but will also keep digging for better way without injecting a custom service just to support test/fixture-specific config overrides.

这篇关于来自集成测试的ASP.NET Core UseSetting的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆