刚用上Moq,用它解决了一个IUnitOfWork的mock问题,在这篇博文中记录一下。
  开发场景
  Application服务层BlogCategoryService的实现代码如下:
public class BlogCategoryService : IBlogCategoryService
{
private IBlogCategoryRepository _blogCategoryRepository;
public BlogCategoryServiceImp(IBlogCategoryRepository blogCategoryRepository)
{
_blogCategoryRepository = blogCategoryRepository;
}
public async Task<IList<BlogCategory>> GetCategoriesAsync(int blogId)
{
return await _blogCategoryRepository.GetCategories(blogId).ToListAsync();
}
}
  这里用到了Entity Framework中System.Data.Entity命名空间下的ToListAsync()扩展方法。
  Repository层BlogCategoryRepository的实现代码如下:
public class BlogCategoryRepository : IBlogCategoryRepository
{
private IQueryable<BlogCategory> _categories;
public BlogCategoryRepository(IUnitOfWork unitOfWork)
{
_categories = unitOfWork.Set<BlogCategory>();
}
public IQueryable<BlogCategory> GetCategories(int blogId)
{
return _categories.Where(c => c.BlogId == blogId);
}
}
  这里在BlogCategoryRepository的构造函数中通过IUnitOfWork接口获取BlogCategory的数据集。
  在单元测试中一开始是这样用Moq对IUnitOfWork接口进行mock的——让IUnitOfWork.Set()方法直接返回IQueryable类型的BlogCategory集合,代码如下:
[Fact]
public async Task GetCategoriesTest()
{
var blogCategories = new List<BlogCategory>()
{
new BlogCategory {  BlogId = 1, Active = true, CategoryId = 1, Title = "C#" },
new BlogCategory {  BlogId = 1, Active = false, CategoryId = 2, Title = "ASP.NET Core" }
}.AsQueryable();
var mockUnitOfWork = new Mock<IUnitOfWork>();
mockUnitOfWork.Setup(u => u.Set<BlogCategory>()).Returns(blogCategories);
_categoryService = new BlogCategoryServiceImp(new BlogCategoryRepository(mockUnitOfWork.Object));
var actual = await _categoryService.GetCategoriesAsync(1);
Assert.Equal(2, actual.Count());
actual.ToList().ForEach(c => Assert.Equal(1, c.BlogId));
}
  遇到问题
  运行单元测试时,却出现下面的错误:
  The source IQueryable doesn't implement IDbAsyncEnumerable<BlogCategory>.
  Only sources that implement IDbAsyncEnumerable can be used for Entity Framework asynchronous operations.
  出现这个错误是由于在BlogCategoryService中用到了EF的ToListAsync()扩展方法,使用这个扩展方法需要实现IDbAsyncEnumerable相关接口,而通过List转换过来的IQueryable并没有实现这个接口。要解决这个问题,我们需要使用实现IDbAsyncEnumerable相关接口的集合类型,而EF中已经天然内置了这样的集合类型,它是DbSet。只要能mock出DbSet,问题迎刃而解。