title: C# Create Mock Configuration.GetSection(“Section:SubSection”) for objects list
body: <h2>Objective</h2>
  <p>Create a mock object, using Moq and XUnit, for loading the specific section &quot;Character/Skills&quot; to enhance the coverage in unit testing.</p>
  <p>The SUT (in some point), loads the setting in the way</p>
  <pre><code>var skills = Configuration.GetSection(“Character:Skills”);
  </code></pre>
  <p>From the following appSetting:</p>
  <pre><code>{
      &quot;dummyConfig1&quot;: {
          &quot;Description&quot;: &quot;bla bla bla...&quot;,
      },
      &quot;Character&quot;: {
      &quot;Name&quot;: &quot;John Wick&quot;,
      &quot;Description&quot;: &quot;A retired hitman seeking vengeance for the killing of the dog given to him...&quot;,
      &quot;Skills&quot;: [
          {
          &quot;Key&quot;: &quot;CQC Combat&quot;,
          &quot;Id&quot;: &quot;15465&quot;
          },
          {
          &quot;Key&quot;: &quot;Firearms&quot;,
          &quot;Id&quot;: &quot;14321&quot;
          },
          {
          &quot;Key&quot;: &quot;Stealth&quot;,
          &quot;Id&quot;: &quot;09674&quot;
          },
          {
          &quot;Key&quot;: &quot;Speed&quot;,
          &quot;Id&quot;: &quot;10203&quot;
          }
      ],
      &quot;DummyConf2&quot;: &quot;more bla bla bla...&quot;
  }
  </code></pre>
  <h2>Previous Reading</h2>
  <p>Reading these posts (and other others, as result of Googling), I noticed that we can only use a primitive &quot;string&quot; datatype or else <em>new Mock&lt;IConfigurationSection&gt;</em> object (with no setting):</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/50201588/c-sharp-how-to-mock-configuration-getsectionfoobar-getliststring">Stack Overflow - how to mock Configuration.GetSection(“foo:bar”)</a>,</li>
  <li><a href="https://www.pmichaels.net/2019/05/04/mocking-iconfiguration-extension-method/" rel="nofollow noreferrer">Mocking IConfiguration extension method</a></li>
  <li><a href="https://dejanstojanovic.net/aspnet/2018/november/mocking-iconfiguration-getvalue-extension-methods-in-unit-test/" rel="nofollow noreferrer">Mocking IConfiguration Getvalue() extension method in Unit Test</a></li>
  </ul>
  <p><strong>Constraint:</strong> 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 &quot;Integration&quot; instead of &quot;Unit&quot;; since there is an I/O dependency.</p>
  <h2>The approach</h2>
  <p>The code's idea (shown later) is mocking each property (key/id) and then merging them in a tree similar to this:</p>
  <ul>
  <li>&quot;Character&quot; ------ Configuration to be read, using <code>GetSection()</code> and then <code>Get&lt;T&gt;()</code>
  <ul>
  <li>&quot;Skills&quot; ------ Configuration list with merged attribute
  <ul>
  <li>&quot;Key&quot; - &quot;CQC Combat&quot;   ------ Primitive value 1</li>
  <li>&quot;Id&quot;  - &quot;15465&quot;        ------ Primitive value 2</li>
  </ul>
  </li>
  </ul>
  </li>
  </ul>
  <h2>The Code</h2>
  <pre><code>var skillsConfiguration = new List&lt;SkillsConfig&gt;
  {
      new SkillsConfig { Key = &quot;CQC Combat&quot;   , Id = &quot;15465&quot; },
      new SkillsConfig { Key = &quot;Firearms&quot;     , Id = &quot;14321&quot; },
      new SkillsConfig { Key = &quot;Stealh&quot;       , Id = &quot;09674&quot; },
      new SkillsConfig { Key = &quot;Speed&quot;        , Id = &quot;10203&quot; },
  };
  
  var configurationMock = new Mock&lt;IConfiguration&gt;();
  var mockConfSections = new List&lt;IConfigurationSection&gt;();
  
  foreach (var skill in skillsConfiguration)
  {
      var index = skillsConfiguration.IndexOf(skill);
  
      //Set the Key string value
      var mockConfSectionKey = new Mock&lt;IConfigurationSection&gt;();
      mockConfSectionKey.Setup(s =&gt; s.Path).Returns($&quot;Character:Skills:{index}:Key&quot;);
      mockConfSectionKey.Setup(s =&gt; s.Key).Returns(&quot;Key&quot;);
      mockConfSectionKey.Setup(s =&gt; s.Value).Returns(skill.Key);
  
      //Set the Id string value
      var mockConfSectionId = new Mock&lt;IConfigurationSection&gt;();
      mockConfSectionId.Setup(s =&gt; s.Path).Returns($&quot;Character:Skills:{index}:Id&quot;);
      mockConfSectionId.Setup(s =&gt; s.Key).Returns(&quot;Id&quot;);
      mockConfSectionId.Setup(s =&gt; s.Value).Returns(skill.Id);
  
      //Merge the attribute &quot;key/id&quot; as Configuration section list
      var mockConfSection = new Mock&lt;IConfigurationSection&gt;();                
      mockConfSection.Setup(s =&gt; s.Path).Returns($&quot;Character:Skills:{index}&quot;);
      mockConfSection.Setup(s =&gt; s.Key).Returns(index.ToString());
      mockConfSection.Setup(s =&gt; s.GetChildren()).Returns(new List&lt;IConfigurationSection&gt; { mockConfSectionKey.Object, mockConfSectionId.Object });    
      
      //Add the skill object with merged attributes
      mockConfSections.Add(mockConfSection.Object);
  }
  
  // Add the Skill's list
  var skillsMockSections = new Mock&lt;IConfigurationSection&gt;();
  skillsMockSections.Setup(cfg =&gt; cfg.Path).Returns(&quot;Character:Skills&quot;);
  skillsMockSections.Setup(cfg =&gt; cfg.Key).Returns(&quot;Skills&quot;);
  skillsMockSections.Setup(cfg =&gt; cfg.GetChildren()).Returns(mockConfSections);
  
  //Mock the whole section, for using GetSection() method withing SUT
  configurationMock.Setup(cfg =&gt; cfg.GetSection(&quot;Character:Skills&quot;)).Returns(skillsMockSections.Object);
  </code></pre>
  <h2>Expected result</h2>
  <p>Running the original system, I get the instantiated list with its respective
  Here is the screenshot:</p>
  <p><a href="https://i.stack.imgur.com/rHnlP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rHnlP.png" alt="appsetting loaded sucessfully" /></a></p>
  <h2>Mocked result</h2>
  <p>The code above, I only get the instantiated list but all attributes return null.
  Here is the screenshot:</p>
  <p><a href="https://i.stack.imgur.com/yu3Vb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yu3Vb.png" alt="appsetting mocked with null values" /></a></p>