title: Moq & EF6 - Mocking EF6 Remove method not removing an object
body: <p>I am trying to mock my EF6 <code>DbContext</code> and it all works for <code>Add</code>, <code>Update</code>, <code>Find</code> methods.  But it's not working for <code>Remove</code> method for unknown reason.</p>
  
  <p>In theory, after removing, the <code>Students</code> collection should have only 1 return left.  But it keeps returning the Count - 2.</p>
  
  <p>I put 3 Moq<code>.Verify</code> checks to ensure that all methods are called and they are executed indeed.  But it's not actually removing the item from the Students Collection.</p>
  
  <p>If I commented <code>Assert.Equal</code> line which check the counts, the whole test passed.</p>
  
  <p><strong>Delete XUnit Method</strong></p>
  
  <pre><code>[Fact]
  public void Delete()
  {            
      Mock&lt;DbContexts.MVCWebAppDbContext&gt; dbContext = new Mock&lt;DbContexts.MVCWebAppDbContext&gt;();
      IStudentsService studentService = new StudentsService(dbContext.Object);
  
      var students = new List&lt;Student&gt;()
      {
          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&lt;DbSet&lt;Student&gt;&gt;();
      mockSet.As&lt;IQueryable&lt;Student&gt;&gt;().Setup(m =&gt; m.Provider).Returns(students.AsQueryable().Provider);
      mockSet.As&lt;IQueryable&lt;Student&gt;&gt;().Setup(m =&gt; m.Expression).Returns(students.AsQueryable().Expression);
      mockSet.As&lt;IQueryable&lt;Student&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(students.AsQueryable().ElementType);
      mockSet.As&lt;IQueryable&lt;Student&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(students.AsQueryable().GetEnumerator());
  
      mockSet.Setup(m =&gt; m.Remove(It.IsAny&lt;Student&gt;())).Callback&lt;Student&gt;((entity) =&gt; students.Remove(entity));
  
      dbContext.Setup(c =&gt; c.Students).Returns(mockSet.Object);
  
      int idToDelete = 1;
  
      dbContext.Setup(s =&gt; s.Students.Find(idToDelete)).Returns(students.Single(s =&gt; s.StudentID == idToDelete));
  
      // call delete method now
      studentService.Delete(idToDelete);
  
      // 1 object deleted, it should return 1
      Assert.Equal(1, students.Count());  // &lt;----- Error here
  
      dbContext.Verify(s =&gt; s.Students.Find(idToDelete), Times.Once);
      dbContext.Verify(s =&gt; s.Students.Remove(It.IsAny&lt;Student&gt;()), Times.Once);
      dbContext.Verify(s =&gt; s.SaveChanges(), Times.Once);
  }
  </code></pre>
  
  <p><strong>StudentService.cs Delete method</strong></p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>Could you guys please help me with this Remove method mocking?</p>