最小起订量测试 Entity Framework

Moq testing Entity Framework

我是 Entity FrameworkMoq testing 的新手。

下面是我的EF code:

public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{

    protected readonly DbContext Context;
    public Repository(DbContext context)
    {
        Context = context;
    }

    public TEntity Get(int id)
    {
      
        return Context.Set<TEntity>().Find(id);
    }

    public IEnumerable<TEntity> GetAll()
    {
       
        return Context.Set<TEntity>().ToList();
    }

    public void Add(TEntity entity)
    {
        Context.Set<TEntity>().Add(entity);
    }

    public void Remove(TEntity entity)
    {
        Context.Set<TEntity>().Remove(entity);
    }

}


public interface IRepository<TEntity> where TEntity : class
{

    TEntity Get(int id);
    IEnumerable<TEntity> GetAll();


    void Add(TEntity entity);

    void Remove(TEntity entity);

}


public interface IUnitOfWork : IDisposable
{

    int Complete();
}


public class UnitOfWork : IUnitOfWork
{


    private readonly EF_SalesOrdersDBContext _context;

    public CustomersRepository customersRepository { get; set; }

    public UnitOfWork(EF_SalesOrdersDBContext context)
    {
        _context = context;
        customersRepository = new CustomersRepository(_context);
    }



    public int Complete()
    {
        return _context.SaveChanges();
    }

    public void Dispose()
    {
        _context.Dispose();
    }
}

public class CustomersRepository : Repository<Customer>
{
    public CustomersRepository(EF_SalesOrdersDBContext context)
        :base(context)
    {
            
    }



}

下方 program.cs 中有效的代码。

   UnitOfWork unitOfWork = new UnitOfWork(new EF_SalesOrdersDBContext());
        unitOfWork.customersRepository.Add(new Customer { CompanyName = "test2", FirstName = "John", LastName = "Barnes", AddressLine1 = "11 lfc", City = "Liverpool", County = "LFC", Phone = "444" });         
        unitOfWork.Complete();

我想使用 Moq 进行测试。

这是我试过的:

 [TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
  
        var mockSet = new Mock<DbSet<Customer>>();

        var mockContext = new Mock<EF_SalesOrdersDBContext>();
        mockContext.Setup(m => m.Customers).Returns(mockSet.Object);


        var service = new CustomersRepository(mockContext.Object);

   

        service.Add(new Customer { CompanyName = "test2", FirstName = "John", LastName = "Barnes", AddressLine1 = "11 lfc", City = "Liverpool", County = "LFC", Phone = "444" });

        mockSet.Verify(m => m.Add(It.IsAny<Customer>()), Times.Once());
        mockContext.Verify(m => m.SaveChanges(), Times.Once());


    }

}

我收到错误:

Message: Test method UnitTestProject1.UnitTest1.TestMethod1 threw exception: System.NullReferenceException: Object reference not set to an instance of an object. Stack Trace: Repository`1.Add(TEntity entity) line 34 UnitTest1.TestMethod1() line 26

模拟数据库上下文是个坏主意 - 您可以简单地使用内存上下文并避免模拟所有内容。它将像真正的数据库一样工作。

查看 Microsoft 的这些建议:https://docs.microsoft.com/en-us/ef/core/testing/

这个问题有一些答案显示了如何创建内存上下文:

使用 Entity Framework 实现存储库模式的一个关键原因是启用单元测试。存储库成为单元测试的抽象点。基本上是这样想的:

  • 您相信 EF 的编写和测试是正确的,可以保证您得到您应该得到的东西。
  • 存储库的目的只是作为中继,不包含高级业务逻辑。
  • 存储库可能执行的依赖于数据状态的任何低级别规则都将包含在集成测试中。这些测试将 运行 使用已知状态的快照数据库或内存数据库。

因此,您的主要业务逻辑应该驻留在您的服务/控制器中,这就是您的单元测试的目标。 Mock 是为您的存储库制作的,而不是 EF DbContexts/DbSets.

因此,例如,根据您的结构,我将有一个 UnitOfWork 的模拟,它断言是否进行了(或不进行)任何 Complete() 调用,然后模拟存储库以获取预期的方法 return 已知状态。