.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": ""
  }
}
相关推荐
星夜离尘5643 分钟前
MATLAB 工程实验:不等间隔流量积分,先把缺口与覆盖率说清楚
开发语言·matlab·时间戳·数据质量·数值积分
_Evan_Yao44 分钟前
Redis 缓存穿透、击穿、雪崩,一次讲清!
java·spring·mybatis
Ai-_Man1 小时前
Meta Muse还是不可以导出左侧所有对话,只能点进去单条内容导出,方法不对吗?
开发语言·人工智能·ai·小程序·c#·1024程序员节
梓豪csd1 小时前
消息订阅中心:谁该收到哪条通知
java·开发语言·人工智能·低代码·自动化·1024程序员节
秋饼1 小时前
Spring AI 2.1 Message Parts 深度实战:推理签名回放与跨轮记忆持久化的源码级落地
java·ai·技术分享·后端开发
学逆向的2 小时前
C 枚举变量
c语言·开发语言·网络安全
Sakura_j_h_2 小时前
Python 在定时任务里输出 emoji,UnicodeEncodeError 从哪来
开发语言·字符编码·windows·python·定时任务
thefool1122662 小时前
TCP 1
java·1024程序员节
金銀銅鐵4 小时前
[Java] 用 GUI 展示 EnumSet 增减元素时的内部变化(下)
java·后端·ai编程
AI人工智能+电脑小能手5 小时前
一次请求打几个下游,这个Java并行任务工具类6个方法按序聚合
java·线程池·并发编程·1024程序员节·批量处理·并行任务