给定以下简单的服务类,在GetCategories()方法中,您应该测试该categoryRepository.Query()方法被调用的事实,还是应该设置一个测试来保存类别列表并返回它们?
我想我要说的是,一旦覆盖了这个测试用例,就会嘲笑categoryRepository并验证它的方法是否被调用?Query
public class CategoryService : ValidatingServiceBase, ICategoryService
{
    private readonly IRepository<Category> categoryRepository;
    private readonly IRepository<SubCategory> subCategoryRepository;
    private readonly IValidationService validationService;
    public CategoryService(
        IRepository<Category> categoryRepository,
        IRepository<SubCategory> subCategoryRepository,
        IValidationService validationService)
        : base(validationService)
    {
        this.categoryRepository = categoryRepository;
        this.subCategoryRepository = subCategoryRepository;
        this.validationService = validationService;
    }
    public IEnumerable<Category> GetCategories()
    {
        return categoryRepository.Query().ToList();
    }
}
样品测试
[Fact]
public void GetCategories_Should_CallRepositoryQuery()
{
    var categoryRepo = new Mock<IRepository<Category>>();
    var service = new CategoryService(categoryRepo.Object, null, null);
    service.GetCategories();
    categoryRepo.Verify(x => x.Query(), Times.Once());
}