.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": ""
  }
}
相关推荐
qq_195551693 分钟前
代码随想录70期day7
java·开发语言
Sam-August31 分钟前
【分布式架构实战】Spring Cloud 与 Dubbo 深度对比:从架构到实战,谁才是微服务的王者?
java·spring cloud·dubbo
麦兜*40 分钟前
MongoDB 常见错误解决方案:从连接失败到主从同步问题
java·数据库·spring boot·redis·mongodb·容器
山楂树下懒猴子1 小时前
ChatAI项目-ChatGPT-SDK组件工程
人工智能·chatgpt·junit·https·log4j·intellij-idea·mybatis
ytadpole1 小时前
揭秘设计模式:命令模式-告别混乱,打造优雅可扩展的代码
java·设计模式
sinat_602035361 小时前
模块与包的导入
运维·服务器·开发语言·python
恋雨QAQ1 小时前
python函数和面向对象
开发语言·python
用户3721574261351 小时前
Java 教程:轻松实现 Excel 与 CSV 互转 (含批量转换)
java
天雪浪子2 小时前
Python入门教程之逻辑运算符
开发语言·python
叫我阿柒啊2 小时前
Java全栈开发实战:从基础到微服务的深度解析
java·微服务·kafka·vue3·springboot·jwt·前端开发