title: Mocking in Unit Tests
body: <p>I am trying to test the <code>AddCategory</code> of the following <code>CategoryService</code>.</p>
  
  <p>My problem is that I am having a hard time understanding what to mock/fake.</p>
  
  <p>My attempt at the test is at the bottom.</p>
  
  <p>I am using MOQ, xUnit and FluentAssertions.</p>
  
  <p>I am using FluentValidation for the validators.</p>
  
  <p><strong>Category Service</strong></p>
  
  <pre><code>public class CategoryService : ValidatingServiceBase, ICategoryService
  {
      private readonly IUnitOfWork unitOfWork;
      private readonly IRepository&lt;Category&gt; categoryRepository;
      private readonly IRepository&lt;SubCategory&gt; subCategoryRepository;
      private readonly IValidationService validationService;
  
      public CategoryService(
          IUnitOfWork unitOfWork,
          IRepository&lt;Category&gt; categoryRepository,
          IRepository&lt;SubCategory&gt; subCategoryRepository,
          IValidationService validationService)
          : base(validationService)
      {
          this.unitOfWork = unitOfWork;
          this.categoryRepository = categoryRepository;
          this.subCategoryRepository = subCategoryRepository;
          this.validationService = validationService;
      }
  
      public bool AddCategory(Category category)
      {
          var validationResult = validationService.Validate(category);
  
          if (!validationResult.IsValid)
          {
              return false;
          }
          else
          {
              categoryRepository.Add(category);
              return true;
          }
      }
  
      public bool DoesCategoryExist(string categoryName)
      {
          return categoryRepository.Query().SingleOrDefault(x =&gt; x.Name == categoryName) != null;
      }
  }
  </code></pre>
  
  <p><strong>Validation Service</strong></p>
  
  <pre><code>public class ValidationService : ServiceBase, IValidationService
  {
      private readonly IValidatorFactory validatorFactory;
  
      public ValidationService(IValidatorFactory validatorFactory)
      {
          Enforce.ArgumentNotNull(validatorFactory, "validatorFactory");
  
          this.validatorFactory = validatorFactory;
      }
  
      public ValidationResult Validate&lt;TEntity&gt;(TEntity entity) where TEntity : class
      {
          var validator = validatorFactory.GetValidator&lt;TEntity&gt;();
          return validator.Validate(entity);
      }
  }
  </code></pre>
  
  <p><strong>Validator Factory</strong></p>
  
  <pre><code>public class ValidatorFactory : IValidatorFactory
  {
      public IValidator GetValidator(Type type)
      {
          Enforce.ArgumentNotNull(type, "type");
  
          return DependencyResolver.Current.GetService(typeof(IValidator&lt;&gt;).MakeGenericType(type)) as IValidator;
      }
  
      public IValidator&lt;T&gt; GetValidator&lt;T&gt;()
      {
          return DependencyResolver.Current.GetService&lt;IValidator&lt;T&gt;&gt;();
      }
  }
  </code></pre>
  
  <p><strong>Category Validator</strong></p>
  
  <pre><code>public class CategoryValidator : AbstractValidator&lt;Category&gt;
  {
      public CategoryValidator(ICategoryService service)
      {
          RuleFor(x =&gt; x.Name)
              .NotEmpty()
              .Must((category, name) =&gt;
              {
                  return service.DoesCategoryExist(name);
              });
      }
  }
  </code></pre>
  
  <p><strong>Unit Test Attempt</strong></p>
  
  <pre><code>[Fact]
      public void AddCategory_Should_ReturnTrue()
      {
          var category = new Category() { Name = "Cat1" };
  
          var unitOfWork = new Mock&lt;IUnitOfWork&gt;();
          var categoryRepo = new Mock&lt;IRepository&lt;Category&gt;&gt;();
          var subCategoryRepo = new Mock&lt;IRepository&lt;SubCategory&gt;&gt;();
  
          var mockCategoryService = new Mock&lt;ICategoryService&gt;();
          var categoryValidator = new CategoryValidator(mockCategoryService.Object);
  
          var validatorFactory = new Mock&lt;IValidatorFactory&gt;();
          validatorFactory.Setup(x =&gt; x.GetValidator&lt;CategoryValidator&gt;()).Returns(categoryValidator as IValidator&lt;CategoryValidator&gt;);
  
          var validationService = new ValidationService(validatorFactory.Object);
  
          var categoryService = new CategoryService(
              unitOfWork.Object,
              categoryRepo.Object,
              subCategoryRepo.Object,
              validationService);
  
          categoryService.AddCategory(category);
      }
  </code></pre>