title: ASPNet.Core HostingEnvironment for Integration Tests?
body: <p>In building the Integration tests for my ASPNet.Core web app, <a href="https://docs.microsoft.com/en-us/aspnet/core/testing/integration-testing" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/aspnet/core/testing/integration-testing</a>,
  I am coming across an issue. The Startup is run when I run the app and the configuration is read and contains all the information in my <code>apsettings.json</code> file.
  Now, when I run the integration test as shown below. the Startup is run but the configuration is different. Why would that happen and how do I make sure it reads 
  the one for the app itself?</p>
  
  <pre><code>[TestFixture]
  class HSMControllerTests
  {
      private readonly TestServer _server;
      private readonly HttpClient _client;
  
      public HSMControllerTests()
      {
          _server = new TestServer(new WebHostBuilder()
          .UseStartup&lt;Startup&gt;());
          _client = _server.CreateClient();
      }
  
      [Test]
      public async global::System.Threading.Tasks.Task GET_PingHSM_ShouldSucceedAsync()
      {
          HttpResponseMessage response = await _client.GetAsync("HSM/PingHSM");
          Assert.NotNull(response);
          Assert.IsInstanceOf&lt;OkObjectResult&gt;(response);
      }
  }
  </code></pre>
  
  <p>I am getting the exception <code>Missing configuration section ServiceConfig.</code></p>
  
  <p>This is how the WebHost is built in <code>Program.cs</code> in my app:</p>
  
  <pre><code>WebHost.CreateDefaultBuilder(args)
         .UseStartup&lt;Startup&gt;()
         .UseKestrel(o =&gt; o.AddServerHeader = false)
         .Build();
  </code></pre>
  
  <p>Could the difference in how it is built in the Test code be the problem?</p>
  
  <p><strong>Modified ControllerTest constructor:</strong></p>
  
  <pre><code>public HSMControllerTests()
  {
      var builder = new ConfigurationBuilder()
          .SetBasePath(Path.GetFullPath(@"../../../../HSM.WebApi.IntegrationTests"))
          .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
          .AddEnvironmentVariables();
  
      Configuration = builder.Build();
  
      _server = new TestServer(WebHost.CreateDefaultBuilder()
          .UseStartup&lt;Startup&gt;());
      _client = _server.CreateClient();
  }
  </code></pre>
  
  <p>Now, how to inject the new Configuration into the Startup? Our Startup is defined like this:</p>
  
  <pre><code>public Startup(IConfiguration configuration, IHostingEnvironment environment)
          : base(typeof(Startup), configuration, environment)
  {
  }
  </code></pre>
  
  <p><strong>modification based on last post by @poke</strong></p>
  
  <pre><code>_server = new TestServer(WebHost.CreateDefaultBuilder()
      .ConfigureAppConfiguration(configBuilder =&gt; new ConfigurationBuilder()
          .SetBasePath(Path.GetFullPath(@"../../../../HSM.WebApi.IntegrationTests"))
          .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
          .AddEnvironmentVariables()
      )
      .UseStartup&lt;Startup&gt;());
  </code></pre>
  
  <p><strong>Got it working, somewhat...</strong></p>
  
  <pre><code>var config = new ConfigurationBuilder()
      .SetBasePath(Path.GetFullPath(@"../../../../HSM.WebApi.IntegrationTests"))
      .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
      .AddEnvironmentVariables();
  
  var Configuration = config.Build();
  
  _server = new TestServer(WebHost.CreateDefaultBuilder()
      .UseConfiguration(Configuration)
      .UseStartup&lt;Startup&gt;());
  _client = _server.CreateClient();
  </code></pre>
  
  <p>But now, the HostingEnvironment is set to the directory of the Tests app vs. the directory of the Web app and it attempts to read to <code>appsettings.json</code> file from there in the HomeController constructor, here:</p>
  
  <pre><code>public HSMController(IHostingEnvironment hostingEnvironment)
  {
      _hostingEnvironment = hostingEnvironment;
      contentRootPath = _hostingEnvironment.ContentRootPath;
  }
  </code></pre>