简体   繁体   English

C# 为对象列表创建 Mock Configuration.GetSection(“Section:SubSection”)

[英]C# Create Mock Configuration.GetSection(“Section:SubSection”) for objects list

Objective客观的

Create a mock object, using Moq and XUnit, for loading the specific section "Character/Skills" to enhance the coverage in unit testing.使用 Moq 和 XUnit 创建一个模拟对象,用于加载特定部分“字符/技能”以增强单元测试的覆盖率。

The SUT (in some point), loads the setting in the way SUT(在某些时候)以这种方式加载设置

var skills = Configuration.GetSection(“Character:Skills”);

From the following appSetting:从以下应用程序设置:

{
    "dummyConfig1": {
        "Description": "bla bla bla...",
    },
    "Character": {
    "Name": "John Wick",
    "Description": "A retired hitman seeking vengeance for the killing of the dog given to him...",
    "Skills": [
        {
        "Key": "CQC Combat",
        "Id": "15465"
        },
        {
        "Key": "Firearms",
        "Id": "14321"
        },
        {
        "Key": "Stealth",
        "Id": "09674"
        },
        {
        "Key": "Speed",
        "Id": "10203"
        }
    ],
    "DummyConf2": "more bla bla bla..."
}

Previous Reading以前的阅读

Reading these posts (and other others, as result of Googling), I noticed that we can only use a primitive "string" datatype or else new Mock<IConfigurationSection> object (with no setting):阅读这些帖子(和其他帖子,谷歌搜索的结果),我注意到我们只能使用原始的“字符串”数据类型或新的 Mock<IConfigurationSection>对象(没有设置):

Constraint: Copying the appSetting file into the TestProject (or create a MemoryStream) to load the real settings could solve this scenario, but the test would be a "Integration" instead of "Unit";约束:将 appSetting 文件复制到 TestProject(或创建 MemoryStream)以加载真实设置可以解决这种情况,但测试将是“集成”而不是“单元”; since there is an I/O dependency.因为存在 I/O 依赖性。

The approach该方法

The code's idea (shown later) is mocking each property (key/id) and then merging them in a tree similar to this:代码的想法(稍后显示)是模拟每个属性(键/ID),然后将它们合并到类似于以下的树中:

  • "Character" ------ Configuration to be read, using GetSection() and then Get<T>() "Character" ------ 要读取的配置,使用GetSection()然后Get<T>()
    • "Skills" ------ Configuration list with merged attribute “技能”------具有合并属性的配置列表
      • "Key" - "CQC Combat" ------ Primitive value 1 “钥匙”——“CQC实战”------原始值1
      • "Id" - "15465" ------ Primitive value 2 "Id" - "15465" ------ 原始值 2

The Code编码

var skillsConfiguration = new List<SkillsConfig>
{
    new SkillsConfig { Key = "CQC Combat"   , Id = "15465" },
    new SkillsConfig { Key = "Firearms"     , Id = "14321" },
    new SkillsConfig { Key = "Stealh"       , Id = "09674" },
    new SkillsConfig { Key = "Speed"        , Id = "10203" },
};

var configurationMock = new Mock<IConfiguration>();
var mockConfSections = new List<IConfigurationSection>();

foreach (var skill in skillsConfiguration)
{
    var index = skillsConfiguration.IndexOf(skill);

    //Set the Key string value
    var mockConfSectionKey = new Mock<IConfigurationSection>();
    mockConfSectionKey.Setup(s => s.Path).Returns($"Character:Skills:{index}:Key");
    mockConfSectionKey.Setup(s => s.Key).Returns("Key");
    mockConfSectionKey.Setup(s => s.Value).Returns(skill.Key);

    //Set the Id string value
    var mockConfSectionId = new Mock<IConfigurationSection>();
    mockConfSectionId.Setup(s => s.Path).Returns($"Character:Skills:{index}:Id");
    mockConfSectionId.Setup(s => s.Key).Returns("Id");
    mockConfSectionId.Setup(s => s.Value).Returns(skill.Id);

    //Merge the attribute "key/id" as Configuration section list
    var mockConfSection = new Mock<IConfigurationSection>();                
    mockConfSection.Setup(s => s.Path).Returns($"Character:Skills:{index}");
    mockConfSection.Setup(s => s.Key).Returns(index.ToString());
    mockConfSection.Setup(s => s.GetChildren()).Returns(new List<IConfigurationSection> { mockConfSectionKey.Object, mockConfSectionId.Object });    
    
    //Add the skill object with merged attributes
    mockConfSections.Add(mockConfSection.Object);
}

// Add the Skill's list
var skillsMockSections = new Mock<IConfigurationSection>();
skillsMockSections.Setup(cfg => cfg.Path).Returns("Character:Skills");
skillsMockSections.Setup(cfg => cfg.Key).Returns("Skills");
skillsMockSections.Setup(cfg => cfg.GetChildren()).Returns(mockConfSections);

//Mock the whole section, for using GetSection() method withing SUT
configurationMock.Setup(cfg => cfg.GetSection("Character:Skills")).Returns(skillsMockSections.Object);

Expected result预期结果

Running the original system, I get the instantiated list with its respective Here is the screenshot:运行原始系统,我得到了各自的实例化列表 这是屏幕截图:

appsetting加载成功

Mocked result模拟结果

The code above, I only get the instantiated list but all attributes return null.上面的代码,我只得到了实例化的列表,但所有的属性都返回空值。 Here is the screenshot:这是屏幕截图:

appsetting 用空值模拟

Finally I refactored the code, getting rid the whole foreach block and replacing the list initialization var mockConfSections = new List<IConfigurationSection>();最后我重构了代码,去掉了整个foreach块并替换了列表初始化var mockConfSections = new List<IConfigurationSection>(); with the follow piece of code, which is simpler and cleaner.使用以下代码,它更简单,更干净。

var fakeSkillSettings = skillsConfiguration.SelectMany(
    skill => new Dictionary<string, string> {
        { $"Character:Skills:{skillsConfiguration.IndexOf(skill)}:Key", skill.Key },
        { $"Character:Skills:{skillsConfiguration.IndexOf(skill)}:Id" , skill.Id  },
});

var configBuilder = new ConfigurationBuilder();
var mockConfSections = configBuilder.AddInMemoryCollection(fakeSkillSettings)
    .Build()
    .GetSection("Character:Skills")
    .GetChildren();

Explanation解释

As the previous implementation built a configuration tree with mocked nodes, there was a need to build a setup and return for each one, resulting in a bloated solution.由于之前的实现构建了带有模拟节点的配置树,因此需要为每个节点构建设置和返回,从而导致解决方案臃肿。

Based on the article Keeping Configuration Settings in Memory , I projected the list with flattened Key/Id Dictionary using the LINQ SelectMany , then built the memory configuration and finally mocked the setting with "real nodes", resulting in one mock setup.根据文章Keeping Configuration Settings in Memory ,我使用LINQ SelectMany用扁平的 Key/Id Dictionary 投影列表,然后构建内存配置,最后用“真实节点”模拟设置,从而产生一个模拟设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 如何模拟 Configuration.GetSection(“foo:bar”).Get <List<string> &gt;() - C# how to mock Configuration.GetSection(“foo:bar”).Get<List<string>>() 如何使用 FakeItEasy 语法模拟 configuration.GetSection? - How to mock configuration.GetSection with FakeItEasy syntax? .NET自定义配置部分:Configuration.GetSection引发“无法找到程序集”异常 - .NET custom configuration section: Configuration.GetSection throws 'unable to locate assembly' exception Configuration.GetSection返回null值 - Configuration.GetSection returns null value 为什么 static Configuration.GetSection() 不可用? - Why is static Configuration.GetSection() not available? .Net Core Configuration.GetSection()。获取&lt;&gt;()不绑定 - .Net Core Configuration.GetSection().Get<>() not binding Configuration.GetSection(“的connectionStringName”)。获取 <?> 总是为空 - Configuration.GetSection(“ConnectionStringName”).Get<?> always null ConfigurationManager.GetSection和Configuration.GetSection有什么区别? - What is Difference between ConfigurationManager.GetSection and Configuration.GetSection? configuration.getValue 或 configuration.getsection 总是返回 null - configuration.getValue or configuration.getsection always returns null Configuration.GetSection()轻松获取原始字符串值,但不获取复杂值 - Configuration.GetSection() easily gets primitive string values but not complex values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM