簡體   English   中英

在VS2017中使用xUnit測試.NET Core 2.0類庫時如何設置ASPNETCORE_ENVIRONMENT

[英]How to set ASPNETCORE_ENVIRONMENT when testing .NET Core 2.0 class library with xUnit in VS2017

我在xUnit測試項目中使用此代碼根據環境加載設置:

public class TestSettings
{
    public string AzureConnectionString { get; }

    public TestSettings(ITestOutputHelper output)
    {
        string envVariable = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

        output.WriteLine($"env: '{envVariable}'");

        IConfigurationRoot config = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddJsonFile($"appsettings.{envVariable}.json", optional: true)
            .Build();
        AzureConnectionString = config.GetConnectionString("AzureStorage");
    }
}

但無論我做什么,我都會envVariable空洞。 在哪里可以為測試項目(VS2017)設置ASPNETCORE_ENVIRONMENT

致ASP.NET核心工具團隊

請使測試項目設置適用於環境。 謝謝。

好的,在Windows上使用此命令設置ASPNETCORE_ENVIRONMENT

setx ASPNETCORE_ENVIRONMENT Development

所以值保持不變(注意'set x ')。

另外,請看這個答案https://stackoverflow.com/a/43951218/2896495

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM