.Net Core 单元测试获取配置文件节点值

单元测试类:

复制代码
ServiceProvider _serviceProvider;
        IConfiguration _config;

        [SetUp]
        public void Setup()
        {
            _config = new ConfigurationBuilder()
            .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
            .Build();

            IServiceCollection services = new ServiceCollection();

            _serviceProvider = services.BuildServiceProvider();

        }

        [Test]
        public void IQueryRecordService_UnitTest()
        {
            var config = _config.Get<AppSettingOptions>();
            var demoModelCodes = config.DemoModelCodes;

            var list = new List<object>();

            if (demoModelCodes != null && demoModelCodes.Count > 0)
            {
                foreach (var item in demoModelCodes)
                {
                    list.Add(Query("阿司匹林8", "110101198708162325", "13145678901", item.ProductLine));
                }
            }

            Console.WriteLine(JsonConvert.SerializeObject(list));

            Assert.True(list.Count > 0);

        }

AppSettingOptions类:

复制代码
public class AppSettingOptions : IAppSetting
    {
        public AppSettingOptions()
        {
            var configBuilder = new ConfigurationBuilder();
            configBuilder.AddJsonFile("appsettings.json");
            var config = configBuilder.Build();
            var section = config.GetSection("AppSettings");
            section.Bind(this);
        }

        public string DemoDB { get; set; }
        public bool OnePageReportCollectionSwitch { get; set; }
        public string[] OnePageReportCollectionFields { get; set; }
        public List<ProductModel> DemoModelCodes { get; set; }
    }


    public class ProductModel
    {
        public string ProductLine { get; set; }
        public string[] StrategyProducts { get; set; }
    }

IAppSetting类:

public interface ISingle
    {
    }

    public interface IScope
    {
    }

    public interface ITrans
    {
    }

public interface IAppSetting : ITrans { }

appsettings.json文件:

复制代码
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "AppSettings": {
    "DemoDB": "Server=;Database=;Integrated Security=SSPI;",
    "OnePageReportCollectionSwitch": false,
    "OnePageReportCollectionFields": [ "Rule_final_weight", "scoreafautofin", "scoreautofin", "scoreautosec", "scoreautocom", "scoreautolea" ],
    "DemogModelCodes": [
      {
        "ProductLine": "BackupLine",
        "StrategyProducts": [ "STR_BR0003543" ]
      }
    ]
  },
  "WebProxy": {
    "Host": "",
    "User": "",
    "Password": ""
  }
}
相关推荐
CryptoRzz9 分钟前
欧美(美股、加拿大股票、墨西哥股票)股票数据接口文档
java·服务器·开发语言·数据库·区块链
杂货铺的小掌柜29 分钟前
apache poi excel 字体数量限制
java·excel·poi
Never_Satisfied32 分钟前
在JavaScript / HTML中,div容器在内容过多时不显示超出的部分
开发语言·javascript·html
大厂码农老A37 分钟前
你打的日志,正在拖垮你的系统:从P4小白到P7专家都是怎么打日志的?
java·前端·后端
艾菜籽1 小时前
Spring MVC入门补充2
java·spring·mvc
艾莉丝努力练剑1 小时前
【C++STL :stack && queue (一) 】STL:stack与queue全解析|深入使用(附高频算法题详解)
linux·开发语言·数据结构·c++·算法
爆更小哇1 小时前
统一功能处理
java·spring boot
程序员鱼皮1 小时前
我造了个程序员练兵场,专治技术焦虑症!
java·计算机·程序员·编程·自学
胡萝卜3.01 小时前
深入理解string底层:手写高效字符串类
开发语言·c++·学习·学习笔记·string类·string模拟实现
n8n1 小时前
SpringAI 完全指南:为Java应用注入生成式AI能力
java·后端