2017-04-14 89 views
4

我想嘲笑我的EF6 DbContext,它的全部作品爲Add,Update, Find方法。但由於未知原因,它不適用於Remove方法。Moq&EF6 - 嘲笑EF6刪除方法不去除一個對象

理論上,刪除後,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

刪除返回什麼值? – Evk

+0

我已調試它,它返回'1'。 – TTCG

回答

4

更換

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)); 

大部分設置的,用它覆蓋上DbSet.Remove

其實設置你其實可以刪除所有DbSet嘲諷和DbContext.Students完成測試依然會通過

[Fact] 
public void Delete() { 
    var 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) } 
    }; 

    dbContext 
     .Setup(m => m.Students.Remove(It.IsAny<Student>())) 
     .Callback<Student>((entity) => students.Remove(entity)); 

    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.AreEqual(1, students.Count()); 

    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); 
}