Using ASP.NET Core's ConfigurationBuilder in a Test Project

久未见 提交于 2020-12-29 05:29:45

问题


I want to use the IHostingEnvironment and ConfigurationBuilder in my functional test project, so that depending on the environment the functional tests run using a different set of configuration. I want to make use of the code below:

public IConfigurationRoot ConfigureConfiguration(IHostingEnvironment hostingEnvironment)
{
    var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json")
        .AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", true)
        .AddEnvironmentVariables();
    return builder.Build();
}

I want to have an appSettings.json and appSettings.Production.json file to point my functional tests at production. Is this possible? How can it be achieved? I would need an instance of IHostingEnvironment.


回答1:


You can use the ConfigurationBuilder in a test project with a couple of steps. I don't think you will need the IHostingEnvironment interface itself.

First, add two NuGet packages to your project which have the ConfigurationBuilder extension methods:

"dependencies": {
  "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc1-final",
  "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final"
}

Second, put your desired environment variables into the test project's properties:

Then you can create your own builder in the test project:

private readonly IConfigurationRoot _configuration;

public BuildConfig()
{
    var environmentName = Environment.GetEnvironmentVariable("Hosting:Environment");

    var config = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json")
        .AddJsonFile($"appsettings.{environmentName}.json", true)
        .AddEnvironmentVariables();

    _configuration = config.Build();
}

If you want to use precisely the same settings file (not a copy), then you'll need to add in a path to it.




回答2:


I'll add this answer here for completeness, as I experienced the same issue as @vaindil describes in Will's answer here. The reason was that we populated our IConfiguration from environment variables in the code under test. This overrode any config we set in test using say an appsettings.json. Our solution was to create environment variables for the test process using System.Environment.SetEnvironvironmentVariable("variableName", "variableValue")

Effectively, the instance of WebHostBuilder in our tests is created the same as our hosted API:

// Code omitted for brevity
var builder = new WebHostBuilder()                 
                .UseEnvironment("Development")
                .ConfigureAppConfiguration(configurationBuilder => configurationBuilder.AddEnvironmentVariables())
                .UseStartup<Startup>();

var testServer = new TestServer(builder); // test against this



回答3:


This is possible, according to the ASP.NET Core documentation (http://docs.asp.net/en/latest/fundamentals/configuration.html) you can build this inside the Startup method and add the Environment variable appSettings. When specifing the optional flag you can use the appSettings.Production.json for production environment and appSettings.json for development when there is no appSettings.Development.json available. The appsettings.{env.EnvironmentName}.json is only used when the file is present and the fallback is the default appsettings.json

public Startup(IHostingEnvironment env)
{
    // Set up configuration providers.
    var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json")
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

    if (env.IsDevelopment())
    {
        // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
        builder.AddUserSecrets();
    }

    builder.AddEnvironmentVariables();
    Configuration = builder.Build();
}



回答4:


I added an appsettings.Development.json to the root of my test project. Then I added the appsettings file via the .ConfigureAppConfiguration() method. It will then call the main application's Startup() method but use the configuration settings in appsettings.Development.json.


    string Environment = "Development";

    var server = new TestServer(new WebHostBuilder()                
        .UseEnvironment(Environment)
        .ConfigureAppConfiguration(x => {
            x.SetBasePath(Directory.GetCurrentDirectory());
            x.AddJsonFile($"appsettings.{Environment}.json", optional: false, reloadOnChange: true);
            x.AddEnvironmentVariables();
            })
        .UseStartup<Startup>()                
        );

    httpClient = server.CreateClient();

The httpClient can then be used to e.g. make web requests such as:

var response = await httpClient.GetAsync(url);
var responseString = await response.Content.ReadAsStringAsync();


来源:https://stackoverflow.com/questions/36943484/using-asp-net-cores-configurationbuilder-in-a-test-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!