gpt4 book ai didi

c# - 运行单元测试时是否可以加载 launchSettings.json 文件中的配置文件?

转载 作者:太空狗 更新时间:2023-10-29 23:43:04 25 4
gpt4 key购买 nike

我正在运行单元测试作为 ASP.NET Core MVC 解决方案的一部分。 .NET Core 的版本准确地说是 2.1。

我在 launchSettings.json 文件中创建了一个配置文件部分,其中包含我希望由测试运行程序加载和注入(inject)的环境变量,以便在运行单元测试时环境变量可用并且可以包含特定值。

我的 ASP.NET Core MVC 项目中的 launchSettings.json 作为链接添加到我的单元测试项目中,属性设置为生成操作 - 无,复制到输出文件夹 - 始终复制。

该文件被复制到我的输出文件夹,但我不确定如何让测试运行器将此文件与 UnitTesting 配置文件一起使用。我曾尝试使用“测试”一词作为配置文件名称,但似乎没有任何效果。

这是一个示例 launchSettings.json 文件:

{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:62267/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"MigrationHistoryTableName": "MigrationHistory",
"ConnectionStringName": "EFConnectionString",
"Redis__SSL": "True",
"Redis__Port": "6380",
"Redis__InstanceName": "RedisDev",
"Redis__AbortConnect": "False",
"Redis__ConnectionString": "{URI}:{Port},password={Password},ssl={SSL},abortConnect={AbortConnect}"
}
},
"MyDataServices": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"MigrationHistoryTableName": "MigrationHistory",
"ConnectionStringName": "EFConnectionString",
"Redis__SSL": "True",
"Redis__Port": "6380",
"Redis__InstanceName": "RedisDev",
"Redis__AbortConnect": "False",
"Redis__ConnectionString": "{URI}:{Port},password={Password},ssl={SSL},abortConnect={AbortConnect}"
},
"applicationUrl": "http://localhost:4080/"
},
"UnitTesting": {
"commandName": "Executable",
"executablePath": "test",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"MigrationHistoryTableName": "MigrationHistory",
"ConnectionStringName": "EFConnectionString",
"Redis__SSL": "True",
"Redis__Port": "6380",
"Redis__InstanceName": "RedisDev",
"Redis__AbortConnect": "False",
"Redis__ConnectionString": "{URI}:{Port},password={Password},ssl={SSL},abortConnect={AbortConnect}"
}
}
}
}

我知道默认情况下 launchSettings.json 文件在 git 中被忽略。我们将使用此文件的开发版本 checkin 我们的代码,该文件将包含预期在开发环境中可用的设置示例。

非常感谢您花时间阅读本文并提供帮助!

最佳答案

我现在写了这个静态加载器,但这并不理想:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace MyNamespaceHere.Services.Common.Utilities
{
public static class LaunchSettingsLoader
{
public static void LaunchSettingsFixture(string launchSettingsPath = "Properties\\launchSettings.json", string profileName = "UnitTesting")
{
using (var file = File.OpenText(launchSettingsPath))
{
var reader = new JsonTextReader(file);
var jObject = JObject.Load(reader);

var allprofiles = jObject
.GetValue("profiles", StringComparison.OrdinalIgnoreCase);

// ideally we use this
var variables = jObject
.GetValue("profiles", StringComparison.OrdinalIgnoreCase)
//select a proper profile here
.SelectMany(profiles => profiles.Children())
//.Where(p => p.Value<String> == profileName)
.SelectMany(profile => profile.Children<JProperty>())
.Where(prop => prop.Name == "environmentVariables")
.SelectMany(prop => prop.Value.Children<JProperty>())
.ToList();

Console.WriteLine(variables?.Count);

var profilesDictJToken = allprofiles.ToObject<Dictionary<string, JToken>>();
var unitTestingProfile = profilesDictJToken[profileName];
var unitTestingProfileDictJToken = unitTestingProfile.ToObject<Dictionary<string, JToken>>();
var environmentVariables = unitTestingProfileDictJToken["environmentVariables"];
var environmentVariablesList = environmentVariables.ToList();

foreach (var variable in environmentVariablesList)
{
var name = ((JProperty)variable).Name;
var value = ((JProperty)variable).Value.ToString();
Environment.SetEnvironmentVariable(name, value);
}
}
}
}
}

关于c# - 运行单元测试时是否可以加载 launchSettings.json 文件中的配置文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53637993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com