title: Reconfigure dependencies when Integration testing ASP.NET Core Web API and EF Core
body: <p>I'm following this tutorial<br>
  <a href="http://www.davepaquette.com/archive/2016/11/27/integration-testing-with-entity-framework-core-and-sql-server.aspx" rel="noreferrer">Integration Testing with Entity Framework Core and SQL Server</a></p>
  
  <p>My code looks like this</p>
  
  <p><strong>Integration Test Class</strong></p>
  
  <pre><code>public class ControllerRequestsShould : IDisposable
  {
      private readonly TestServer _server;
      private readonly HttpClient _client;
      private readonly YourContext _context;
  
      public ControllerRequestsShould()
      {
          // Arrange
          var serviceProvider = new ServiceCollection()
              .AddEntityFrameworkSqlServer()
              .BuildServiceProvider();
  
          var builder = new DbContextOptionsBuilder&lt;YourContext&gt;();
  
          builder.UseSqlServer($"Server=(localdb)\\mssqllocaldb;Database=your_db_{Guid.NewGuid()};Trusted_Connection=True;MultipleActiveResultSets=true")
              .UseInternalServiceProvider(serviceProvider);
  
          _context = new YourContext(builder.Options);
          _context.Database.Migrate();
  
          _server = new TestServer(new WebHostBuilder()
              .UseStartup&lt;Startup&gt;()
              .UseEnvironment(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")));
          _client = _server.CreateClient();
      }
  
      [Fact]
      public async Task ReturnListOfObjectDtos()
      {
          // Arrange database data
          _context.ObjectDbSet.Add(new ObjectEntity{ Id = 1, Code = "PTF0001", Name = "Portfolio One" });
          _context.ObjectDbSet.Add(new ObjectEntity{ Id = 2, Code = "PTF0002", Name = "Portfolio Two" });
  
          // Act
          var response = await _client.GetAsync("/api/route");
          response.EnsureSuccessStatusCode();
  
  
          // Assert
          var result = Assert.IsType&lt;OkResult&gt;(response);            
      }
  
      public void Dispose()
      {
          _context.Dispose();
      }
  </code></pre>
  
  <p>As I understand it, the <code>.UseStartUp</code> method ensures the <code>TestServer</code> uses my startup class</p>
  
  <p>The issue I'm having is that when my Act statement is hit</p>
  
  <pre><code>var response = await _client.GetAsync("/api/route");
  </code></pre>
  
  <p>I get an error in my startup class that the connection string is null.  I think My understanding of the problem is that when my controller is hit from the client it injects my data repository, which in turn injects the db context.</p>
  
  <p>I think I need to configure the service as part of the <code>new WebHostBuilder</code> section so that it used the context created in the test. But I'm not sure how to do this.</p>
  
  <p><strong>ConfigureServices method in Startup.cs</strong></p>
  
  <pre><code>        public void ConfigureServices(IServiceCollection services)
      {
          // Add framework services
          services.AddMvc(setupAction =&gt;
          {
              setupAction.ReturnHttpNotAcceptable = true;
              setupAction.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
              setupAction.InputFormatters.Add(new XmlDataContractSerializerInputFormatter());
          });
  
          // Db context configuration
          var connectionString = Configuration["ConnectionStrings:YourConnectionString"];
          services.AddDbContext&lt;YourContext&gt;(options =&gt; options.UseSqlServer(connectionString));
  
          // Register services for dependency injection
          services.AddScoped&lt;IYourRepository, YourRepository&gt;();
      }
  </code></pre>