title: How to unit test Command Handler in CQRS pattern in C#
body: <p>I'm learning and practicing CQRS and Unit Test in C#. I need to know how to unit test Command Handlers in CQRS.</p>
  <p>Here is my <code>CreateAuthorCommand</code>:</p>
  <pre class="lang-cs prettyprint-override"><code>public sealed class CreateAuthorCommand : ICommand&lt;Author&gt;
  {
      public CreateAuthorCommand(string firstName, string lastName, DateTimeOffset dateOfBirth, string mainCategory, IEnumerable&lt;CreateBookDto&gt; books)
      {
          FirstName = firstName;
          LastName = lastName;
          DateOfBirth = dateOfBirth;
          MainCategory = mainCategory;
          Books = books;
      }
  
      public string FirstName { get; }
      public string LastName { get; }
      public DateTimeOffset DateOfBirth { get; private set; }
      public string MainCategory { get; }
      public IEnumerable&lt;CreateBookDto&gt; Books { get; }
  
      [AuditLog]
      [DatabaseRetry]
      internal sealed class AddCommandHandler : ICommandHandler&lt;CreateAuthorCommand, Author&gt;
      {
          private readonly IUnitOfWork _unitOfWork;
  
          public AddCommandHandler(IUnitOfWork unitOfWork)
          {
              _unitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork));
          }
  
          public async Task&lt;Result&lt;Author&gt;&gt; Handle(CreateAuthorCommand command)
          {
              var nameResult = Name.Create(command.FirstName, command.LastName);
              var birthDateResult = BirthDate.Create(command.DateOfBirth);
              var mainCategoryResult = Entities.Authors.MainCategory.Create(command.MainCategory);
  
              var authorResult = Result.Combine(nameResult, birthDateResult, mainCategoryResult)
                                  .Map(() =&gt; new Author(nameResult.Value, birthDateResult.Value, null, mainCategoryResult.Value));
                  
              if (authorResult.IsFailure)
                  return Result.Failure&lt;Author&gt;(authorResult.Error);
  
              await _unitOfWork.AuthorRepository.AddAsync(authorResult.Value);
  
              await _unitOfWork.SaveChangesAsync();
  
              return Result.Success(authorResult.Value);
          }
      }
  }
  </code></pre>
  <p>Here is my Unit Test:</p>
  <pre class="lang-cs prettyprint-override"><code>public class CreateAuthorCommandTests
  {
      [Fact]
      public void Create_Author_Should_Call_Add_Method_Once() 
      {
          var fixture = new Fixture();
          var command = fixture.Create&lt;CreateAuthorCommand&gt;();
          var mockUnitOfWork = new Mock&lt;IUnitOfWork&gt;();
          var mockHandler = new Mock&lt;ICommandHandler&lt;CreateAuthorCommand, Author&gt;&gt;();
  
          var result = mockHandler.Object.Handle(command);
  
          mockUnitOfWork.Verify(x =&gt; x.AuthorRepository.AddAsync(It.IsAny&lt;Author&gt;()), Times.Once);
      }
  }
  </code></pre>
  <p>When I debug the above test,</p>
  <ol>
  <li>I'm not able to step into Handler Method!!</li>
  <li>How to pass <code>mockUnitOfWork</code> as constructor parameter to <code>AddCommandHandler</code>?</li>
  </ol>
  <p>If I can pass <code>mockUnitOfWork</code>, the I can verify the <code>AddAsync</code> call inside my <code>AuthorRepository</code>. Please assist on what I'm doing wrong.</p>