使用Moq对Entity Framework通用存储库进行单元测试

11

问题/难题

我无法通过测试,因为通用仓库this.dbSet = context.Set<T>();始终为null。如下面的代码所示,我已经模拟了DbSet和上下文。我还设置了模拟的上下文返回模拟的DbSet。正如预期的那样,EnityRepository构造函数使用了模拟上下文,但是this.dbSet = context.Set<T>();没有选择我的模拟DbSet。我不确定我做错了什么。我没有正确地模拟吗?

结构:

  • DAL - 实体框架,通用存储库,工作单元
  • BLL - 服务,自动映射器(将生成的实体类/对象映射到业务对象)
  • 接口 - IService
  • Model - 业务对象
  • Web - ASP.NET MVC
  • Test - 单元测试

通用存储库

public class EntityRepository<T> : IEntityRepository<T> where T : class
{
    internal MyDB_Entities context;
    internal DbSet<T> dbSet;

    public EntityRepository(MyDB_Entities context)
    {
        this.context = context;
        this.dbSet = context.Set<T>();
    }

    public virtual T GetByID(object id)
    {
        return dbSet.Find(id);
    }

    // more code
}

通用仓库接口

public interface IEntityRepository<T> where T : class
{ 
    IEnumerable<T> Get(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "");
    T GetByID(object id);
    // more code
}

工作单元

public class UnitOfWork : IUnitOfWork, IDisposable
{
    MyDB_Entities _context;
    public IEntityRepository<Customer> customerRepository { get; set; }
    public IEntityRepository<Product> productRepository { get; set; }

    public UnitOfWork(MyDB_Entities context)
    {
        _context = context;
        customerRepository = new EntityRepository<Customer>(_context);
        productRepository = new EntityRepository<Product>(_context); 
    }

    public void Save()
    {
        _context.SaveChanges();
    }
    // more code
}

工作单元接口

public interface IUnitOfWork
{
    IEntityRepository<Customer> customerRepository { get; set; }
    IEntityRepository<Product> productRepository { get; set; }
    void Dispose();
    void Save();
}

服务

public class SomeService : ISomeService 
{
    readonly IUnitOfWork _unitOfWork;
    public SomeService (IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }
    // DoSomethingMethod
}

服务接口

public interface ISomeService
{
    // IDoSomethingMethod 
}

扩展

public static class MockDBSetExtension
{
    public static void SetSource<T>(this Mock<DbSet<T>> mockSet, IList<T> source) where T : class
    {
        var data = source.AsQueryable();
        mockSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(data.Provider);
        mockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(data.Expression);
        mockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(data.ElementType);
        mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
    }
}

测试类

[TestClass]
public class My_Test
{
    Mock<DbSet<Product>> _mockProductDBSet;
    Mock<MyDB_Entities> mockContext;

    [TestInitialize]
    public void TestInitialize()
    {
        _mockProductDBSet = new Mock<DbSet<Product>>();
        mockContext = new Mock<MyDB_Entities>();
        mockContext.Setup(s => s.Products).Returns(_mockProductDBSet.Object);
    }

    [TestMethod]
    public void TestMocking()
    {
       var prod = new Product() { ProductName= "AAA", ProductID = 1 };
        _mockProductDBSet.SetSource(new List<Product> { prod });
       // more code here (new up the service, then test the service method, etc)
    }
}

被测试的系统是什么?因为基于你所拥有的接口,如果被测试的系统只是引用这些接口,那么你就不需要模拟DbSet或者DbContext。它们并没有被它们各自的接口所暴露出来。 - Nkosi
目前,我只是尝试在测试之前模拟虚假数据/记录。我可以在服务类中放置一个方法调用作为我的SUT并模拟IUnitOfWork然后进行测试。理论上这是可行的,我现在不太担心。我更关心的是能否模拟一些虚假记录...但显然失败了 =( - NKD
澄清一下 - 我的目标只是从产品仓储中调用GetByID(1)并能够看到我创建的虚拟记录。您建议如何进行测试而不使用模拟上下文和dbset? - NKD
1个回答

15

假设你有一个定义为IProuctService的服务接口

public interface IProductService {
    string GetProductName(int productId);
}

具体实现取决于 IUnitOfWork 接口。

public class ProductService : IProductService {
    readonly IUnitOfWork _unitOfWork;
    public ProductService(IUnitOfWork unitOfWork) {
        _unitOfWork = unitOfWork;
    }

    public string GetProductName(int productId) {
        var item = _unitOfWork.productRepository.GetByID(productId);

        if (item != null) {
            return item.ProductName;
        }

        throw new ArgumentException("Invalid product id");
    }
}

如果要测试的方法是 IProductService.GetProductName,这里是一个可以进行的测试示例。

[TestMethod]
public void ProductService_Given_Product_Id_Should_Get_Product_Name() {
    //Arrange
    var productId = 1;
    var expected = "AAA";
    var product = new Product() { ProductName = expected, ProductID = productId };

    var productRepositoryMock = new Mock<IEntityRepository<Product>>();
    productRepositoryMock.Setup(m => m.GetByID(productId)).Returns(product).Verifiable();

    var unitOfWorkMock = new Mock<IUnitOfWork>();
    unitOfWorkMock.Setup(m => m.productRepository).Returns(productRepositoryMock.Object);

    IProductService sut = new ProductService(unitOfWorkMock.Object);
    //Act
    var actual = sut.GetProductName(productId);

    //Assert
    productRepositoryMock.Verify();//verify that GetByID was called based on setup.
    Assert.IsNotNull(actual);//assert that a result was returned
    Assert.AreEqual(expected, actual);//assert that actual result was as expected
}
在这种情况下,无需模拟DbSet或DbContext,因为SUT不需要实现相关接口。它们可以被模拟以供系统测试使用。

谢谢您查看这个。这个解决方案绝对达到了目标。我计划在TestInitialize()中模拟DbSet和DbContext,并最终将var prod = new Product() { ProductName= "AAA", ProductID = 1 };和_mockProductDBSet.SetSource(new List<Product> { prod });也移到TestInitialize()。这样,我就不必为每个测试重复创建一个虚假产品的代码了。正如您所见,我为此编写了扩展代码和TestInitialize()。 - NKD
从你的话中我理解为这是一种错误的单元测试方式?我是否在任何时候都不应该模拟dbset/dbcontext?是否有一个好的场景需要模拟dbset和dbcontext? - NKD
2
并不是说这样做是错误的,只是你应该尽量避免嘲弄那些你无法控制且不属于你所有的类。微软已经对这些类进行了彻底的测试。你可能只需要在实际调用数据库时才会用到它们。 - Nkosi
我听你说。就减少在每个测试中重复创建假产品而言,我会简单地使用全局变量,然后在 TestInitialize() 中添加 product = new Product() { ... }; 来在每个测试中重复使用。不必要模拟整个 dbcontext 和 dbset。谢谢你的时间! - NKD
从SUT/ProductService的角度来看,断言productRepositoryMock.Verify();有什么好处?这难道不会测试实现细节,从而阻碍重构吗? - officer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接