我正在尝试模拟我的EF6 DbContext
,这一切都适用于Add
,Update
,Find
方法。但由于未知原因,它无法用于Remove
方法。
理论上,删除后,Students
集合应该只剩下1个返回。但它不断返回伯爵 - 2。
我进行了3次Moq .Verify
检查,以确保调用所有方法并确实执行它们。但它实际上并没有从学生集合中删除该项目。
如果我评论Assert.Equal
行检查计数,则整个测试通过。
删除XUnit方法
[Fact]
public void Delete()
{
Mock<DbContexts.MVCWebAppDbContext> dbContext = new Mock<DbContexts.MVCWebAppDbContext>();
IStudentsService studentService = new StudentsService(dbContext.Object);
var students = new List<Student>()
{
new Student() { StudentID = 1, RefNo = "12456343", FirstName = "John", LastName = "Smith", DateOfBirth = DateTime.Now.AddYears(-10), DateCreated = DateTime.Now },
new Student() { StudentID = 2, RefNo = "87984564", FirstName = "Pete", LastName = "Luck", DateOfBirth = DateTime.Now.AddYears(-20), DateCreated = DateTime.Now.AddDays(1) }
};
var mockSet = new Mock<DbSet<Student>>();
mockSet.As<IQueryable<Student>>().Setup(m => m.Provider).Returns(students.AsQueryable().Provider);
mockSet.As<IQueryable<Student>>().Setup(m => m.Expression).Returns(students.AsQueryable().Expression);
mockSet.As<IQueryable<Student>>().Setup(m => m.ElementType).Returns(students.AsQueryable().ElementType);
mockSet.As<IQueryable<Student>>().Setup(m => m.GetEnumerator()).Returns(students.AsQueryable().GetEnumerator());
mockSet.Setup(m => m.Remove(It.IsAny<Student>())).Callback<Student>((entity) => students.Remove(entity));
dbContext.Setup(c => c.Students).Returns(mockSet.Object);
int idToDelete = 1;
dbContext.Setup(s => s.Students.Find(idToDelete)).Returns(students.Single(s => s.StudentID == idToDelete));
// call delete method now
studentService.Delete(idToDelete);
// 1 object deleted, it should return 1
Assert.Equal(1, students.Count()); // <----- Error here
dbContext.Verify(s => s.Students.Find(idToDelete), Times.Once);
dbContext.Verify(s => s.Students.Remove(It.IsAny<Student>()), Times.Once);
dbContext.Verify(s => s.SaveChanges(), Times.Once);
}
StudentService.cs删除方法
MVCWebAppDbContext _context;
public StudentsService(MVCWebAppDbContext context)
{
_context = context;
}
public int Delete(int id)
{
var objToDelete = _context.Students.Find(id);
if (objToDelete != null)
{
_context.Students.Remove(objToDelete);
return _context.SaveChanges();
}
return -1;
}
你能帮我解决这个删除方法吗?
答案 0 :(得分:6)
替换
Node: CursorKind.FUNCTION_DECL, 'lookup' (type: 'int (int &)')
Node: CursorKind.PARM_DECL, 'm' (type: 'int &')
与
mockSet
.Setup(m => m.Remove(It.IsAny<Student>()))
.Callback<Student>((entity) => students.Remove(entity));
大多数设置都是使用覆盖dbContext
.Setup(m => m.Students.Remove(It.IsAny<Student>()))
.Callback<Student>((entity) => students.Remove(entity));
DbContext.Students
完成的
实际上你实际上可以删除所有DbSet.Remove
模拟而测试仍会传递
DbSet