title: Dependency Injection with XUnit Mediatr and IServiceCollection
body: <p>Currently I'm able to handle IServiceCollection to inject mocks for particular services in the following manner.</p>
  
  <pre><code>public class TestClass
  {
      private IMediator _mediatr;
  
      private void SetupProvider(IUnitOfWork unitOfWork, ILogger logger)
      {
          configuration = new ConfigurationBuilder().Build();
          _services = new ServiceCollection();
          _services.AddSingleton(configuration);
          _services.AddScoped(x =&gt; unitOfWork);
          _services.AddSingleton(logger);
          _services.AddMediatR(Assembly.Load("Application"));
          _services.AddScoped(typeof(IPipelineBehavior&lt;,&gt;), typeof(LoggerBehaviour&lt;,&gt;));
          _mediator = _services.BuildServiceProvider().GetService&lt;IMediator&gt;();
      }
  
      [Fact]
      public async void UnitTest_Success()
      {
          var unitOfWork = new Mock&lt;IUnitOfWork&gt;();
          var logger = new Mock&lt;ILogger&gt;();
          SetupProvider(unitOfWork.Object, logger.Object);
          var fixture = new Fixture();
          var command = fixture.Create&lt;MediatorCommand&gt;();
  
          unitOfWork.Setup(x =&gt; x.Repository.FindAll(It.IsAny&lt;IList&lt;long&gt;&gt;(), It.IsAny&lt;bool?&gt;()))
          .ReturnsAsync(new List&lt;Domain.Model&gt;());
  
          var response = await _mediatr.Send(command);
  
          using (new AssertionScope())
          {
              response.Should().NotBeNull();
              response.IsSuccess.Should().BeTrue();
          }
      }
  }
  </code></pre>
  
  <p>For the following subject under test</p>
  
  <pre><code>public class MediatorCommand : IRequest&lt;CommandResponse&gt;
  {
      public string Name { get; set ;}
      public string Address { get; set; }
  }
  
  public class MediatorCommandHandler : IRequestHandler&lt;MediatorCommand, CommandResponse&gt;
  {
      private readonly ILogger _logger;
      private readonly IUnitOfWork _unitOfWork;
  
      public MediatorCommandHandler(IUnitOfWork unitOfWork, ILogger logger)
      {
          _logger = logger;
          _unitOfWork = unitOfWork;
      }
  
      public async Task&lt;CommandResponse&gt; Handle(MediatorCommand command, CancellationToken cancellationToken)
      {
          var result = new CommandResponse { IsSuccess = false };
          try
          {
              var entity = GetEntityFromCommand(command);
              await _unitOfWork.Save(entity);
              result.IsSuccess = true;
          }
          catch(Exception ex)
          {
              _logger.LogError(ex, ex.Message);
          }
  
          return result;
      }
  }
  </code></pre>
  
  <p>This test runs fine and the unitOfWork and logger mocks are used in the command handlers.</p>
  
  <p>I'm try to move this so that the IServiceCollection construction happens per class instead of each test using the following:</p>
  
  <pre><code>public class SetupFixture : IDisposable
  {
      public IServiceCollection _services;
      public IMediator Mediator { get; private set; }
      public Mock&lt;IUnitOfWork&gt; UnitOfWork { get; private set; }
  
      public SetupFixtureBase()
      {
          UnitOfWork = new Mock&lt;IUnitOfWork&gt;();
          configuration = new ConfigurationBuilder().Build();
          _services = new ServiceCollection();
          _services.AddSingleton(configuration);
          _services.AddScoped(x =&gt; UnitOfWork);
          _services.AddSingleton(new Mock&lt;ILogger&gt;().Object);
          _services.AddMediatR(Assembly.Load("Application"));
          _services.AddScoped(typeof(IPipelineBehavior&lt;,&gt;), typeof(LoggerBehaviour&lt;,&gt;));
          Mediator = _services.BuildServiceProvider().GetService&lt;IMediator&gt;();
      }
  
      public void Dispose()
      {
          Mediator = null;
          _services.Clear();
          _services = null;
      }
  }
  
  public class TestClass : IClassFixture&lt;SetupFixture&gt;
  {
      protected readonly SetupFixture _setupFixture;
  
      public UnitTestBase(SetupFixture setupFixture)
      {
          _setupFixture = setupFixture;
      }
  
      [Fact]
      public async void UnitTest_Success()
      {
          var fixture = new Fixture();
          var command = fixture.Create&lt;MediatorCommand&gt;();
  
          _setupFixture.UnitOfWork.Setup(x =&gt; x.Repository.FindAll(It.IsAny&lt;IList&lt;long&gt;&gt;(), It.IsAny&lt;bool?&gt;()))
          .ReturnsAsync(new List&lt;Domain.Model&gt;());
  
          var response = await _mediatr.Send(command);
  
          using (new AssertionScope())
          {
              response.Should().NotBeNull();
              response.IsSuccess.Should().BeTrue();
          }
      }
  }
  </code></pre>
  
  <p>Unfortunately with this method my mocks do not get injected on the command handler.  Is there a way to get this to work?</p>
  
  <p>Thank you,</p>