.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": ""
  }
}
相关推荐
呜呼~2251421 分钟前
前后端数据交互
java·vue.js·spring boot·前端框架·intellij-idea·交互·css3
神雕杨23 分钟前
node js 过滤空白行
开发语言·前端·javascript
飞的肖29 分钟前
从测试服务器手动热部署到生产环境的实现
java·服务器·系统架构
周伯通*35 分钟前
策略模式以及优化
java·前端·策略模式
两点王爷43 分钟前
Java读取csv文件内容,保存到sqlite数据库中
java·数据库·sqlite·csv
lvbu_2024war011 小时前
MATLAB语言的网络编程
开发语言·后端·golang
问道飞鱼1 小时前
【Springboot知识】Springboot进阶-实现CAS完整流程
java·spring boot·后端·cas
抓哇小菜鸡1 小时前
WebSocket
java·websocket
single5941 小时前
【c++笔试强训】(第四十五篇)
java·开发语言·数据结构·c++·算法
游客5201 小时前
自动化办公-合并多个excel
开发语言·python·自动化·excel