title: Using and injecting from appsettings.json in unit tests on .net core
body: <p>I have created an asp.net core web application and a unit test application.</p>
  
  <p>I created the asp.net application using the "ASP.NET Core Web Application (.NET Core)" template and created the unit test project using the "Class Library (.NET Core)" template.</p>
  
  <p>I configured MSTest using the instructions on the following article:</p>
  
  <p><a href="https://blogs.msdn.microsoft.com/visualstudioalm/2016/05/30/announcing-mstest-framework-support-for-net-core-rc2-asp-net-core-rc2/" rel="noreferrer">Announcing MSTest Framework support for .NET Core RC2 / ASP.NET Core RC2</a></p>
  
  <p>I have organised the application into Controllers and Services, the controllers read values from appsettings.json and pass these into the service methods as arguments.</p>
  
  <p>I have an AppSettings class as follows</p>
  
  <pre><code>public class AppSettings
  {
      public string Setting1 { get; set; }
      public string Setting2 { get; set; }
      public string Setting3etc { get; set; }
  }
  
  public static class App
  {
      public static AppSettings Settings { get; set; }
  
      public static void ConfigureSettings(IOptions&lt;AppSettings&gt; settings)
      {
          Settings = settings.Value; 
      }
  }
  </code></pre>
  
  <p>The controller constructor is as follows</p>
  
  <pre><code>public ValuesController(IOptions&lt;AppSettings&gt; settings)
  {            
      App.ConfigureSettings(settings);
  }
  </code></pre>
  
  <p>In Startup.cs I have the following line in the <code>ConfigureServices</code> method</p>
  
  <pre><code>services.Configure&lt;AppSettings&gt;(Configuration.GetSection("AppSettings"));
  </code></pre>
  
  <p>I learned about this technique from the following article</p>
  
  <p><a href="https://weblog.west-wind.com/posts/2016/may/23/strongly-typed-configuration-settings-in-aspnet-core" rel="noreferrer">Strongly Typed Configuration Settings in ASP.NET Core</a></p>
  
  <p>This works at run time, but I'm having difficulties accessing these settings in my unit tests.</p>
  
  <p>I'm trying to find a way to get the values from appsettings.json into my test project, so that I can pass them to the controllers and services.</p>
  
  <p>My test methods look something like this</p>
  
  <pre><code>[TestMethod]
  [TestCategory("Service - Method1")]
  public void ServiceMethod1Test()
  {
      // this compiles but gets null reference exception
      var setting1 = App.Settings.Setting1;
      var setting2 = App.Settings.Setting2;
  
      var service = new Service(setting1, setting2);
  
      var argument1 = "argument";
      var actual = service.Method1(argument1);
      var expected = "expected result";
  
      CollectionAssert.AreEqual(expected, actual);
  }
  
  [TestMethod]
  [TestCategory("Controller - Method1")]
  public void ControllerMethod1Test()
  {
      // how do i create this settings instance?
      var settings = ???
  
      var controler = new ValuesController(settings);
  
      var argument1 = "argument";
  
      var actual = controller.Method1(argument1);
      var expected = "expected result";
  
      CollectionAssert.AreEqual(expected, actual);
  }
  </code></pre>
  
  <p>How could I go about passing an instance of a class that implements 
  <code>IOptions&lt;MySettings&gt;</code> to the controller constructor for controller tests, and how could I go about getting those values to pass to service methods in the service tests. </p>