title: Mocking EF DbContext with Moq
body: <p>I'm trying to create a unit test for my service with a mocked DbContext. I created an interface <code>IDbContext</code> with the following functions:</p>
  
  <pre><code>public interface IDbContext : IDisposable
  {
      IDbSet&lt;T&gt; Set&lt;T&gt;() where T : class;
      DbEntityEntry&lt;T&gt; Entry&lt;T&gt;(T entity) where T : class;
      int SaveChanges();
  }
  </code></pre>
  
  <p>My real context implements this interface <code>IDbContext</code> and <code>DbContext</code>.</p>
  
  <p>Now I'm trying to mock the <code>IDbSet&lt;T&gt;</code> in the context, so it returns a <code>List&lt;User&gt;</code> instead.</p>
  
  <pre><code>[TestMethod]
  public void TestGetAllUsers()
  {
      // Arrange
      var mock = new Mock&lt;IDbContext&gt;();
      mock.Setup(x =&gt; x.Set&lt;User&gt;())
          .Returns(new List&lt;User&gt;
          {
              new User { ID = 1 }
          });
  
      UserService userService = new UserService(mock.Object);
  
      // Act
      var allUsers = userService.GetAllUsers();
  
      // Assert
      Assert.AreEqual(1, allUsers.Count());
  }
  </code></pre>
  
  <p>I always get this error on <code>.Returns</code>:</p>
  
  <pre><code>The best overloaded method match for
  'Moq.Language.IReturns&lt;AuthAPI.Repositories.IDbContext,System.Data.Entity.IDbSet&lt;AuthAPI.Models.Entities.User&gt;&gt;.Returns(System.Func&lt;System.Data.Entity.IDbSet&lt;AuthAPI.Models.Entities.User&gt;&gt;)'
  has some invalid arguments
  </code></pre>