title: Writing better fixtures for unit tests in .Net Core
body: <p>I'm fairly new to unit testing in the .Net Core. I'm writing unit tests for an <code>Interactor</code> class, whose responsibility is to get<code>Client</code>s either by email or name using the <code>IClientGateway</code> dependency. I've used <code>XUnit</code>, <code>Moq.AutoMock</code> and <code>FluentAssertions</code>.</p>
  <h3><code>GetClientsInteractorTest.cs</code></h3>
  <pre class="lang-cs prettyprint-override"><code>[Trait(&quot;Clients&quot;, &quot;GetClientsInteractor&quot;)]
  [Collection(nameof(GetClientsInteractorTestsCollectionFixture))]
  public class GetClientsInteractorTests
  {
      private readonly GetClientsInteractorTestsFixture fixture;
  
      public GetClientsInteractorTests(GetClientsInteractorTestsFixture fixture) =&gt;
          this.fixture = fixture;
  
      [Theory(DisplayName = &quot;should return clients in clientsGateway&quot;)]
      [MemberData(nameof(GetClientsInteractorTestsFixture.Params), MemberType = typeof(GetClientsInteractorTestsFixture))]
      public async Task ShouldReturnClients(IEnumerable&lt;Client&gt; mockedClients, CancellationTokenSource cancellationTokenSource,
                                              GetClientsRequest request, GetClientsResponse expectedResponse)
      {
          // Arrange
          fixture.Mocker.GetMock&lt;IClientGateway&gt;()
              .Setup(ClientGateway =&gt; ClientGateway.GetByNameAsync(request.Name, cancellationTokenSource.Token))
              .ReturnsAsync(mockedClients.Where(client =&gt; client.Name.Equals(request.Name)))
              .Verifiable();
          fixture.Mocker.GetMock&lt;IClientGateway&gt;()
              .Setup(ClientGateway =&gt; ClientGateway.GetByEmailAsync(request.Email, cancellationTokenSource.Token))
              .ReturnsAsync(mockedClients.Where(client =&gt; client.Email.Equals(request.Email)))
              .Verifiable();
          // Act
          GetClientsResponse actualResponse =
              await fixture.Mocker.CreateInstance&lt;GetClientsInteractor&gt;()
              .Handle(request, cancellationTokenSource.Token);
          // Assert
          if(request.Name != null)
              fixture.Mocker.GetMock&lt;IClientGateway&gt;()
                  .Verify(mock =&gt; mock.GetByNameAsync(request.Name, cancellationTokenSource.Token), Times.Once(), &quot;GetByNameAsync was not called&quot;);
          else if (request.Email != null)
              fixture.Mocker.GetMock&lt;IClientGateway&gt;()
                  .Verify(mock =&gt; mock.GetByEmailAsync(request.Email, cancellationTokenSource.Token), Times.Once(), &quot;GetByEmailAsync was not called&quot;);
          actualResponse.Should()
              .Be(expectedResponse);
      }
  }
  </code></pre>
  <h3><code>GetClientsInteractorTestsFixture.cs</code></h3>
  <pre class="lang-cs prettyprint-override"><code>public class GetClientsInteractorTestsFixture
  {
      public AutoMocker Mocker = new AutoMocker(MockBehavior.Strict);
  
      public static IEnumerable&lt;Client&gt; mockedClients = new Client[] {
              new Client()
              {
                  ClientId = 1,
                  Name = &quot;Victor&quot;,
                  Email = &quot;someEmail@someProvider.com&quot;
              },
          };
  
      public static CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
  
      public static IEnumerable&lt;object[]&gt; Params = new[]
      {
          new object[]
          {
              mockedClients,
              cancellationTokenSource,
              new GetClientsRequest(),
              new GetClientsResponse()
          },
  
          new object[]
          {
              mockedClients,
              cancellationTokenSource,
              new GetClientsRequest(){ Name = &quot;Victor&quot; },
              new GetClientsResponse(){ Clients = new List&lt;ClientDto&gt;()
                  {
                      new ClientDto()
                      {
                          Id = 1,
                          Name = &quot;Victor&quot;,
                          Email = &quot;someEmail@someProvider.com&quot;
                      }
                  }
              }
          },
  
          new object[]
          {
              mockedClients,
              cancellationTokenSource,
              new GetClientsRequest(){ Email = &quot;someEmail@someProvider.com&quot;},
              new GetClientsResponse(){ Clients = new List&lt;ClientDto&gt;()
                  {
                      new ClientDto()
                      {
                          Id = 1,
                          Name = &quot;Victor&quot;,
                          Email = &quot;someEmail@someProvider.com&quot;
                      }
                  }
              }
          },
      };
  }
  </code></pre>
  <p>The unit test itself (<code>ShouldReturnClients</code>) its quite simple at first, but it requires 4 parameters (plus <code>AutoMocker</code>) as a test fixture, found in <code>GetClientsInteractorTestsFixture</code>. Is there a better way to make this test fixture by making it less repetitive? perhaps using something like <code>AutoFixture</code>?</p>