.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": ""
  }
}
相关推荐
MessiGo20 分钟前
Javascript 编程基础(5)面向对象 | 5.2、原型系统
开发语言·javascript·原型模式
慢半拍iii1 小时前
数据结构——D/串
c语言·开发语言·数据结构·c++
bxlj_jcj1 小时前
深入剖析Debezium:CDC领域的“数据魔法棒”
java·架构
叶 落2 小时前
ubuntu 安装 JDK8
java·ubuntu·jdk·安装·java8
爱学习的白杨树2 小时前
Sentinel介绍
java·开发语言
Frankabcdefgh2 小时前
Python基础数据类型与运算符全面解析
开发语言·数据结构·python·面试
XW2 小时前
java mcp client调用 (modelcontextprotocol)
java·llm
kaiaaaa2 小时前
算法训练第十五天
开发语言·python·算法
南玖i3 小时前
vue3 + ant 实现 tree默认展开,筛选对应数据打开,简单~直接cv
开发语言·前端·javascript
南枝异客3 小时前
三数之和-力扣
开发语言·javascript·数据结构·算法·leetcode·排序算法