我是 Entity Framework
和 Moq 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());
}
}
我收到错误:
<块引用>留言: 测试方法 UnitTestProject1.UnitTest1.TestMethod1 抛出异常: System.NullReferenceException:未将对象引用设置为对象的实例。堆栈跟踪: Repository`1.Add(TEntity entity) 第34行 UnitTest1.TestMethod1() 第 26 行
答案 0 :(得分:2)
模拟 db 上下文是一个坏主意 - 您可以简单地使用内存上下文并避免模拟所有内容。它会像真正的数据库一样工作。
查看 Microsoft 的这些建议:https://docs.microsoft.com/en-us/ef/core/testing/
这个问题的一些答案展示了如何制作内存上下文:Unit testing with EF Core and in memory database
答案 1 :(得分:0)
使用实体框架实现存储库模式的一个关键原因是启用单元测试。存储库成为单元测试的抽象点。基本上是这样想的:
因此,您的主要业务逻辑应该驻留在服务/控制器中的位置,这就是您的单元测试的目标位置。 Mocks 是为您的存储库制作的,而不是 EF DbContexts/DbSets。
因此,例如,鉴于您的结构,我将有一个 UnitOfWork 的模拟,它断言是否进行了任何 Complete()
调用(或不进行),然后模拟存储库的预期方法以返回已知状态。< /p>