【发布时间】:2018-12-05 17:47:04
【问题描述】:
我正在运行单元测试作为 ASP.NET Core MVC 解决方案的一部分。 .NET Core 的版本准确地说是 2.1。
我在 launchSettings.json 文件中创建了一个配置文件部分,其中包含我希望由测试运行程序加载和注入的环境变量,以便环境变量可用并且在运行单元测试时可以包含特定值。
我的 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}"
}
}
}
}
我了解默认情况下,git 中会忽略 launchSettings.json 文件。我们将使用此文件的开发版本签入我们的代码,该文件将包含预期在开发环境中可用的设置示例。
感谢您抽出宝贵时间阅读本文并提供帮助!
【问题讨论】:
-
目前,我创建了一个从文件加载配置文件并将环境变量设置为测试设置夹具的一部分的方法,但如果它,我宁愿让框架为我做这件事是可能的。
-
你想获取具体的 UnitTesting 配置文件吗?
-
是的,那太棒了。
标签: c# asp.net-core .net-core