我正在运行单元测试作为ASP.NET核心MVC解决方案的一部分.确切地说,.NET Core的版本是2.1.
我在launchSettings.json文件中创建了一个配置文件部分,其中包含我希望由测试运行器加载和注入的环境变量,以便环境变量可用并且在运行单元测试时可以包含特定值.
我的ASP.NET Core MVC项目中的launchSettings.json作为链接添加到我的单元测试项目中,并且属性设置为Build Action – None,copy to output folder – copy Always.
该文件被复制到我的输出文件夹,但我不知道如何让测试运行器将此文件与UnitTesting配置文件一起使用.我尝试使用“测试”这个词作为配置文件名称,似乎没有任何效果.
{ "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",abortConnect={AbortConnect}" },"applicationUrl": "http://localhost:4080/" },"UnitTesting": { "commandName": "Executable","executablePath": "test",abortConnect={AbortConnect}" } } } }
据我所知,默认情况下,git中会忽略launchSettings.json文件.我们将使用此文件的开发版本检入我们的代码,该文件将包含预期在开发环境中可用的设置示例.
谢谢你花时间阅读本文并提供帮助!
解决方法
我现在写了这个静态加载器,但这并不理想:
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); } } } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。