title: Mock IConfigurationSection to return array of string
body: <p>Using <a href="https://stackoverflow.com/questions/50201588/c-sharp-how-to-mock-configuration-getsectionfoobar-getliststring">this</a> answer to mock the <code>IConfiguration</code> methods from an ASP.NET Core app.</p>
  <p>I need to mock an <code>IConfigurationSection</code> to return a <code>string</code> array.</p>
  <p>My configuration class looks like this:</p>
  <pre class="lang-cs prettyprint-override"><code>public class LoggingConfiguration
  {
      public string ApplicationName { get; set; }
      public string[] Loggers { get; set; }
  }
  </code></pre>
  <p>appsettings.json</p>
  <pre><code>{
      &quot;Logging&quot;: {
          &quot;LoggingConfiguration&quot;: {
                &quot;ApplicationName&quot;: &quot;Some app&quot;,
                &quot;Loggers&quot;:  [ &quot;DiskLogger&quot;, &quot;MemoryLogger&quot;],
                &quot;DiskLogger&quot;: {
                    &quot;SomeSettingOne&quot; : &quot;setting&quot;,
                    &quot;SomeSettingTwo&quot; : &quot;setting&quot;,
                 },
                &quot;MemoryLogger&quot;: {
                    &quot;AnotherSetting&quot;: &quot;...&quot;,
                 }
         }
  </code></pre>
  <p>In setting up the mocks - I have two problems.</p>
  <ol>
  <li>I can't figure out how to mock an <code>IConfigurationSection</code> that would return <code>string[]</code> (<code>loggers</code>)</li>
  <li>I am getting an exception when I try to setup the <code>GetChildren()</code> method on the <code>LoggingSectionMock</code></li>
  </ol>
  <pre class="lang-cs prettyprint-override"><code>public void Setup()
  {
      var applicationNameConfigurationSectionMock = new Mock&lt;IConfigurationSection&gt;();
      applicationNameConfigurationSectionMock.Setup(m =&gt; m.Value).Returns(TestingApplicationName);
  
      var loggerNamesConfigurationSectionMock = new Mock&lt;IConfigurationSection&gt;();
      loggerNamesConfigurationSectionMock.Setup(m =&gt; m.GetChildren()).Returns(GetLoggerNamesSection);
  
      //Throwing Method Not Found exception
      LoggingSectionMock.Setup(m =&gt; m.GetChildren()).Returns(new List&lt;IConfigurationSection&gt;
          {applicationNameConfigurationSectionMock.Object, loggerNamesConfigurationSectionMock.Object});
  
      ConfigurationMock.Setup(m =&gt; m.GetSection($&quot;{Logging}:{LoggingConfiguration}&quot;))
          .Returns(() =&gt; LoggingSectionMock.Object);
  }
  
  private IEnumerable&lt;IConfigurationSection&gt; GetLoggerNamesSection()
  {
      var loggerNamesConfigurationSections = new List&lt;IConfigurationSection&gt;();
      LoggerNames.ToList().ForEach(loggerName =&gt;
      {
          var configSectionMock = new Mock&lt;IConfigurationSection&gt;();
          configSectionMock.Setup(m =&gt; m.Value).Returns(loggerName);
          loggerNamesConfigurationSections.Add(configSectionMock.Object);
      });
      return loggerNamesConfigurationSections;
  }
  </code></pre>