title: ContosoUniversityDotNetCore-Pages SliceFixture with AutoMapper
body: <p>My Create handler uses <code>AutoMapper</code> to map from command to entity.</p>
  <pre><code>public async Task&lt;int&gt; Handle(Command command, CancellationToken cancellationToken)
  {
      var entity = _mapper.Map&lt;ExampleEntity&gt;(command);
      await _db.ExampleEntity.AddAsync(entity, cancellationToken);
      await _db.SaveChangesAsync(cancellationToken);
      return entity.Id;
  }
  </code></pre>
  <p>My test looks like this</p>
  <pre><code>var command = new Create.Command
  {
      Name = &quot;Example 1&quot;,
       ...
  };
  var id = await _fixture.SendAsync(command, mapper);
  id.ShouldNotBeNull();
  </code></pre>
  <p>This is the SliceFixture</p>
  <pre><code>[CollectionDefinition(nameof(SliceFixture))]
  public class SliceFixtureCollection : ICollectionFixture&lt;SliceFixture&gt; { }
  
  public class SliceFixture : IAsyncLifetime
  {
      private readonly Checkpoint _checkpoint;
      private readonly IConfiguration _configuration;
      private readonly IServiceScopeFactory _scopeFactory;
      private readonly WebApplicationFactory&lt;Startup&gt; _factory;
  
      public SliceFixture()
      {
          _factory = new ContosoTestApplicationFactory();
          _configuration = _factory.Services.GetRequiredService&lt;IConfiguration&gt;();
          _scopeFactory = _factory.Services.GetRequiredService&lt;IServiceScopeFactory&gt;();
          _checkpoint = new Checkpoint();
      }
  
      public class ContosoTestApplicationFactory : WebApplicationFactory&lt;Startup&gt;
      {
          protected override void ConfigureWebHost(IWebHostBuilder builder)
          {
              builder.ConfigureAppConfiguration((_, configBuilder) =&gt;
              {
                  configBuilder.AddInMemoryCollection(new Dictionary&lt;string, string&gt;
                  {
                      {&quot;ConnectionStrings:DefaultConnection&quot;, _connectionString}
                  });
              });
          }
  
          private readonly string _connectionString = &quot;&quot;;
      }
  
      public async Task ExecuteScopeAsync(Func&lt;IServiceProvider, Task&gt; action)
      {
          using var scope = _scopeFactory.CreateScope();
          var dbContext = scope.ServiceProvider.GetRequiredService&lt;SchoolContext&gt;();
  
          try
          {
              await dbContext.BeginTransactionAsync();
              await action(scope.ServiceProvider);
              await dbContext.CommitTransactionAsync();
          }
          catch (Exception)
          {
              dbContext.RollbackTransaction(); 
              throw;
          }
      }
  
      public async Task&lt;T&gt; ExecuteScopeAsync&lt;T&gt;(Func&lt;IServiceProvider, Task&lt;T&gt;&gt; action)
      {
          using var scope = _scopeFactory.CreateScope();
          var dbContext = scope.ServiceProvider.GetRequiredService&lt;SchoolContext&gt;();
  
          try
          {
              await dbContext.BeginTransactionAsync();
              var result = await action(scope.ServiceProvider);
              await dbContext.CommitTransactionAsync();
              return result;
          }
          catch (Exception)
          {
              dbContext.RollbackTransaction();
              throw;
          }
      }
  
      public Task ExecuteDbContextAsync(Func&lt;SchoolContext, Task&gt; action) =&gt; ExecuteScopeAsync(sp =&gt; action(sp.GetService&lt;SchoolContext&gt;()));
  
      public Task ExecuteDbContextAsync(Func&lt;SchoolContext, ValueTask&gt; action) =&gt; ExecuteScopeAsync(sp =&gt; action(sp.GetService&lt;SchoolContext&gt;()).AsTask());
  
      public Task ExecuteDbContextAsync(Func&lt;SchoolContext, IMediator, Task&gt; action) =&gt; ExecuteScopeAsync(sp =&gt; action(sp.GetService&lt;SchoolContext&gt;(), sp.GetService&lt;IMediator&gt;()));
  
      public Task&lt;T&gt; ExecuteDbContextAsync&lt;T&gt;(Func&lt;SchoolContext, Task&lt;T&gt;&gt; action) =&gt; ExecuteScopeAsync(sp =&gt; action(sp.GetService&lt;SchoolContext&gt;()));
  
      public Task&lt;T&gt; ExecuteDbContextAsync&lt;T&gt;(Func&lt;SchoolContext, ValueTask&lt;T&gt;&gt; action) =&gt; ExecuteScopeAsync(sp =&gt; action(sp.GetService&lt;SchoolContext&gt;()).AsTask());
  
      public Task&lt;T&gt; ExecuteDbContextAsync&lt;T&gt;(Func&lt;SchoolContext, IMediator, Task&lt;T&gt;&gt; action) =&gt; ExecuteScopeAsync(sp =&gt; action(sp.GetService&lt;SchoolContext&gt;(), sp.GetService&lt;IMediator&gt;()));
  
      public Task InsertAsync&lt;T&gt;(params T[] entities) where T : class
      {
          return ExecuteDbContextAsync(db =&gt;
          {
              foreach (var entity in entities)
              {
                  db.Set&lt;T&gt;().Add(entity);
              }
              return db.SaveChangesAsync();
          });
      }
  
      public Task&lt;T&gt; FindAsync&lt;T&gt;(int id)
          where T : class, IEntity
      {
          return ExecuteDbContextAsync(db =&gt; db.Set&lt;T&gt;().FindAsync(id).AsTask());
      }
  
      public Task&lt;TResponse&gt; SendAsync&lt;TResponse&gt;(IRequest&lt;TResponse&gt; request)
      {
          return ExecuteScopeAsync(sp =&gt;
          {
              var mediator = sp.GetRequiredService&lt;IMediator&gt;();
  
              return mediator.Send(request);
          });
      }
  
      public Task SendAsync(IRequest request)
      {
          return ExecuteScopeAsync(sp =&gt;
          {
              var mediator = sp.GetRequiredService&lt;IMediator&gt;();
  
              return mediator.Send(request);
          });
      }
  
      private int _courseNumber = 1;
  
      public int NextCourseNumber() =&gt; Interlocked.Increment(ref _courseNumber);
  
      public Task InitializeAsync() =&gt; _checkpoint.Reset(_configuration.GetConnectionString(&quot;DefaultConnection&quot;));
  
      public Task DisposeAsync()
      {
          _factory?.Dispose();
          return Task.CompletedTask;
      }
  }
  
  </code></pre>
  <p>When I run the test I get -</p>
  <blockquote>
  <p>Object reference not set to an instance of an object.</p>
  </blockquote>
  <p>because <code>_mapper</code> is null.</p>
  <p>I know I probably need to modify the <code>SliceFixture</code> but can't figure out what to do.</p>
  <p>I was looking to implement Jimmy’s example <a href="https://github.com/jbogard/ContosoUniversityDotNetCore-Pages/blob/master/ContosoUniversity.IntegrationTests/SliceFixture.cs" rel="nofollow noreferrer">here</a> with the additional <code>AutoMapper</code> implementation.</p>