簡體   English   中英

集成測試中的C#環境變量

[英]C# Environment variables in integration test

我已經構建了一個自定義Web應用程序Factory來運行內存測試服務器以進行集成測試。 我們嘗試使用sql數據庫,而不是在內存數據庫中使用。

我的問題是每個環境的連接字符串都在變化。 我們有幾個帶有連接字符串的appsettings {envName} .json文件。 所以,我需要找到一個獲取env變量的解決方案。 這些是我的嘗試:

public class CustomWebApplicationSQLServerFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.ConfigureServices(services =>
        {
            var serviceProvider = new ServiceCollection()
                .AddEntityFrameworkSqlServer()
                .BuildServiceProvider();

            var env = serviceProvider.GetService<IHostingEnvironment>();

            Configuration = new ConfigurationBuilder()
                .SetBasePath(AppContext.BaseDirectory)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables()
                .Build();

            var defaultConnection = Configuration.GetConnectionString("DefaultConnection");
            var connectionString = string.Format(defaultConnection, "TestSampleOrder_" + Guid.NewGuid().ToString());

            services.AddDbContext<SampleOrderContext>(options =>
            {
                options.UseSqlServer(connectionString);
                options.UseInternalServiceProvider(serviceProvider);
            });

            var sp = services.BuildServiceProvider();

            using (var scope = sp.CreateScope())
            {
                var scopedServices = scope.ServiceProvider;

                var context = scopedServices.GetRequiredService<SampleOrderContext>();

                DbInitializer.InitializeDbForTests(context);
            }
        });
    }

另外,我在我的測試Project中添加了一個launchSettings.json文件。 在該文件中,在我的測試項目名稱下,我添加了“ASPNETCORE_ENVIRONMENT”:“AnyValue”。 所以,我無法理解為什么env = null 進行更多測試后,env.EnvironmentName =“Development”,應用程序名稱和路徑是主項目。 為什么? “開發”字未在孔解決方案中設置。

我也試過替換這個的env行:

var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

如果我設置了一個Windows變量,我可以使用它來獲取值,但我需要了解如何在launchSettings文件中設置變量的值。

我嘗試過的另一件事是:

var serviceProvider = services.BuildServiceProvider();

但我真的不明白其中的區別。

任何人都可以幫助我理解這是如何工作的,獲取env變量的最佳方法是什么,使用services.BuildServiceProvider()和new ServiceCollection()之間有什么區別?

非常感謝。

也許這個問題可以提供一些見解。

話雖這么說,用配置進行測試的“最簡單”的方法是抽象你需要的東西。 也許您可以構建一個為您提供環境名稱的適配器,例如:

public interface IApplicationConfiguration
{
    string GetEnvironmentName();
}

public class ApplicationConfiguration
{
    private IHostingEnvironment _hostingConfiguration;

    public ApplicationConfiguration(IHostingEnvironment hostingConfiguration)
    {
        _hostingConfiguration = hostingConfiguration;
    }

    public string GetEnvironmentName()
    {
        return _hostingConfiguration.EnvironmentName;
    }
}

可以使用其他選項解決它,但模擬這些類型的設置可能非常有用。

暫無
暫無

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

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