title: ASP.NET Core Integration Testing & Mocking using Moq
body: <p>I have the following <a href="https://docs.microsoft.com/en-gb/aspnet/core/test/integration-tests?view=aspnetcore-2.2" rel="noreferrer">ASP.NET Core integration test</a> using a custom <code>WebApplicationFactory</code> </p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>public class CustomWebApplicationFactory&lt;TEntryPoint&gt; : WebApplicationFactory&lt;TEntryPoint&gt;
      where TEntryPoint : class
  {
      public CustomWebApplicationFactory()
      {
          this.ClientOptions.AllowAutoRedirect = false;
          this.ClientOptions.BaseAddress = new Uri("https://localhost");
      }
  
      public ApplicationOptions ApplicationOptions { get; private set; }
  
      public Mock&lt;IClockService&gt; ClockServiceMock { get; private set; }
  
      public void VerifyAllMocks() =&gt; Mock.VerifyAll(this.ClockServiceMock);
  
      protected override TestServer CreateServer(IWebHostBuilder builder)
      {
          this.ClockServiceMock = new Mock&lt;IClockService&gt;(MockBehavior.Strict);
  
          builder
              .UseEnvironment("Testing")
              .ConfigureTestServices(
                  services =&gt;
                  {
                      services.AddSingleton(this.ClockServiceMock.Object);
                  });
  
          var testServer = base.CreateServer(builder);
  
          using (var serviceScope = testServer.Host.Services.CreateScope())
          {
              var serviceProvider = serviceScope.ServiceProvider;
              this.ApplicationOptions = serviceProvider.GetRequiredService&lt;IOptions&lt;ApplicationOptions&gt;&gt;().Value;
          }
  
          return testServer;
      }
  }
  </code></pre>
  
  <p>which looks like it should work but the problem is that the <code>ConfigureTestServices</code> method is never being called, so my mock is never registered with the IoC container. You can find the full source code <a href="https://github.com/Dotnet-Boxed/Templates/tree/cursor-paging-in-api-template/Source/ApiTemplate" rel="noreferrer">here</a>.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class FooControllerTest : IClassFixture&lt;CustomWebApplicationFactory&lt;Startup&gt;&gt;, IDisposable
  {
      private readonly HttpClient client;
      private readonly CustomWebApplicationFactory&lt;Startup&gt; factory;
      private readonly Mock&lt;IClockService&gt; clockServiceMock;
  
      public FooControllerTest(CustomWebApplicationFactory&lt;Startup&gt; factory)
      {
          this.factory = factory;
          this.client = factory.CreateClient();
          this.clockServiceMock = this.factory.ClockServiceMock;
      }
  
      [Fact]
      public async Task Delete_FooFound_Returns204NoContent()
      {
          this.clockServiceMock.SetupGet(x =&gt; x.UtcNow).ReturnsAsync(new DateTimeOffset.UtcNow);
  
          var response = await this.client.DeleteAsync("/foo/1");
  
          Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
      }
  
      public void Dispose() =&gt; this.factory.VerifyAllMocks();
  }
  </code></pre>