无法使DbSet.Find与Moq一起使用(使用实体框架) [英] Cannot get DbSet.Find to work with Moq (Using the Entity-Framework)
本文介绍了无法使DbSet.Find与Moq一起使用(使用实体框架)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于某种原因,此代码不断失败.任何可以告诉我原因的人:
For some reason this code keeps failing. Anyone that can tell me why:
var activeLoans = new List<ActiveLoan> {
new ActiveLoan{
ID = 1,
CaseType = "STL",
LoanCode = 0
},
new ActiveLoan{
ID = 2,
CaseType = "STL",
LoanCode = 0
},
new ActiveLoan{
ID = 3,
CaseType = "STL",
LoanCode = 0
}
}.AsQueryable();
var activeLoanMockSet = new Mock<DbSet<ActiveLoan>>();
activeLoanMockSet.As<IQueryable<ActiveLoan>>().Setup(m => m.Provider).Returns(activeLoans.Provider);
activeLoanMockSet.As<IQueryable<ActiveLoan>>().Setup(m => m.Expression).Returns(activeLoans.Expression);
activeLoanMockSet.As<IQueryable<ActiveLoan>>().Setup(m => m.ElementType).Returns(activeLoans.ElementType);
activeLoanMockSet.As<IQueryable<ActiveLoan>>().Setup(m => m.GetEnumerator()).Returns(activeLoans.GetEnumerator());
mockContext.Setup(c => c.ActiveLoans).Returns(activeLoanMockSet.Object);
// This is the line that fails
Assert.AreNotEqual(mockContext.Object.ActiveLoans.Find( 1 ), null);
当我说失败"时,是指这是失败的一部分的单元测试.
When I say fail i mean that the unit test that this is a part of fails.
推荐答案
我认为您还需要设置 查看全文