title: Unit Test Mock a class inheriting multiple interfaces and a class
body: <p>First the code,</p>
  
  <p><em>Generic Interface:</em></p>
  
  <pre><code>public interface IEntityService&lt;TEntity&gt; where TEntity : class
  {
      IQueryable&lt;TEntity&gt; Get(Expression&lt;Func&lt;TEntity, bool&gt;&gt; filter = null,
        Func&lt;IQueryable&lt;TEntity&gt;, IOrderedQueryable&lt;TEntity&gt;&gt; orderBy = null,
        string includeProperties = "");
  
      Task&lt;TEntity&gt; GetByIDAsync(object id);
  
      Task&lt;TEntity&gt; GetFirstOrDefaultAsync(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate);
  }
  </code></pre>
  
  <p><em>Generic Class with interface implementation:</em></p>
  
  <pre><code>public class EntityService&lt;TEntity&gt; : IEntityService&lt;TEntity&gt; where TEntity : class
  {
      protected IContext IContext;
      protected DbSet&lt;TEntity&gt; IDbSet;
  
      public EntityService(IContext context)
      {
          IContext = context;
          IDbSet = IContext.Set&lt;TEntity&gt;();
      }
  
      public virtual IQueryable&lt;TEntity&gt; Get(Expression&lt;Func&lt;TEntity, bool&gt;&gt; filter = null,
          Func&lt;IQueryable&lt;TEntity&gt;, IOrderedQueryable&lt;TEntity&gt;&gt; orderBy = null,
          string includeProperties = "")
      {
          IQueryable&lt;TEntity&gt; query = IDbSet;
  
          if (filter != null)
          {
              query = query.Where(filter);
          }
  
          query = includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Aggregate(query, (current, includeProperty) =&gt; current.Include(includeProperty));
  
          if (orderBy != null)
          {
              return orderBy(query);
          }
          return query;
      }
  
      public virtual async Task&lt;TEntity&gt; GetByIDAsync(object id)
      {
          return await IDbSet.FindAsync(id);
      }
  
      public virtual async Task&lt;TEntity&gt; GetFirstOrDefaultAsync(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate)
      {
          return await IDbSet.FirstOrDefaultAsync(predicate);
      }
  }
  </code></pre>
  
  <p><em>Specific interface:</em></p>
  
  <pre><code>public interface ILoginService
  {
      Task&lt;UserProfileViewModel&gt; GetLoginDetailAsync(string userName);
  }
  </code></pre>
  
  <p><em>Specific class: Implementing generic class and specific interface</em></p>
  
  <pre><code>public class LoginService : EntityService&lt;UserAccount&gt;, ILoginService
  {
  
      private readonly IContext _iContext;
  
      public LoginService(IContext context): base(context)
      {
          _iContext = context;
      }
  
      async Task&lt;UserProfileViewModel&gt; ILoginService.GetLoginDetailAsync(string userName)
      {
          var userAcount = await GetFirstOrDefaultAsync(c =&gt; c.Username.ToLower() == userName.Trim().ToLower() &amp;&amp; c.Active == true);
          if (userAcount != null)
          {
              return Mapper.Map&lt;UserAccount, UserProfileViewModel&gt;(userAcount);
          }
          return null;
      }
  }
  </code></pre>
  
  <p>Now, I am supposed to test <code>LoginService</code> the one and only method it has</p>
  
  <p>Here's the test code</p>
  
  <pre><code>    [Test]
      public async Task GetLoginDetailAsync_InvalidUsername_ReturnsNull()
      {
          var userName = "should not exist!";
          var userAccount = new List&lt;UserAccount&gt;()
          {
              new UserAccount
              {
                  ID = 1,
                  Name = "Test User"
              }
          }.AsQueryable();
          var mockSet = new Mock&lt;DbSet&lt;UserAccount&gt;&gt;();
          var userProfileViewModel = new UserProfileViewModel
          {
              ID = 1,
              Name = Guid.NewGuid().ToString().Substring(0, 8)
          };
          _context.Setup(c =&gt; c.Set&lt;UserAccount&gt;()).Returns(mockSet.Object);
          loginService = new LoginService(_context.Object);
          mockSet.As&lt;IDbAsyncEnumerable&lt;UserAccount&gt;&gt;().
              Setup(m =&gt; m.GetAsyncEnumerator()).
              Returns(new TestDbAsyncEnumerator&lt;UserAccount&gt;(userAccount.GetEnumerator()));
          mockSet.As&lt;IQueryable&lt;UserAccount&gt;&gt;()
              .Setup(m =&gt; m.Provider)
              .Returns(new TestDbAsyncQueryProvider&lt;UserAccount&gt;(userAccount.Provider));
          mockSet.As&lt;IQueryable&lt;UserAccount&gt;&gt;().Setup(m =&gt; m.Expression).Returns(userAccount.Expression);
          mockSet.As&lt;IQueryable&lt;UserAccount&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(userAccount.ElementType);
          mockSet.As&lt;IQueryable&lt;UserAccount&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(userAccount.GetEnumerator());
  
          var result = await ((ILoginService)loginService).GetLoginDetailAsync(userName);
          Assert.IsNull(result);
      }
  </code></pre>
  
  <p>Now, these <code>TestDbAsyncEnumerator</code> and <code>TestDbAsyncQueryProvider</code> are taken from <a href="https://msdn.microsoft.com/en-us/library/dn314429(v=vs.113).aspx" rel="nofollow noreferrer">msdn</a> to test <code>Async</code> queries in EF.</p>
  
  <p><strong>The problem</strong></p>
  
  <p>The test throws an exception, that <code>Message: System.NotImplementedException : The member 'IQueryable.Provider' has not been implemented on type 'DbSet1Proxy' which inherits from 'DbSet1'. Test doubles for 'DbSet1' must provide implementations of methods and properties that are used.</code> Basically, I have not setup the <code>FirstOrDefaultAsync</code> for <code>mockSet</code> that is getting called in <code>GetLoginDetailAsync</code> (it calls to <code>EntityService</code>, that ends up calling the <code>FirstOrDefaultAsync</code> of <code>IDbSet</code>). </p>
  
  <p>I don't know how can I mock that, because the <code>LoginService</code> doesn't directly inherits it. It inherit's the <code>EntityService</code> which in turn has that generic method <code>FirstOrDefaultAsync</code>. I am stuck at how to set up that.</p>
  
  <p>One another thing that I thought was try this</p>
  
  <pre><code>var loginMock = new Mock&lt;LoginService&gt;(_context.Object);
  loginMock.As&lt;ILoginService&gt;().Setup(c =&gt; c.GetLoginDetailAsync(It.IsAny&lt;string&gt;())).Returns(Task.FromResult&lt;UserProfileViewModel&gt;(null));
  loginMock.As&lt;IEntityService&lt;UserAccount&gt;&gt;().Setup(c =&gt; c.GetFirstOrDefaultAsync(It.IsAny&lt;Expression&lt;Func&lt;UserAccount, bool&gt;&gt;&gt;())).Returns(Task.FromResult(userAccount.First()));
  </code></pre>
  
  <p>But I don't think this is the correct way to go, as I would only be testing the mock object. Can anyone suggest me how do I get to setup and test/mock this <code>GetFirstOrDefaultAsync</code>, or am I totally going in a wrong direction?</p>
  
  <p><strong>UPDATE AFTER ANSWER:</strong>  </p>
  
  <p>After the answer from @ODawgG, I am updating this. The test worked fine as specified in answer, but now the other test is failing. I wanted to test, if a particular user exits in the system.</p>
  
  <p>Here's the test code:
        [Test]
          public async Task Test3()
          {</p>
  
  <pre><code>        var userAccount = new List&lt;UserAccount&gt;()
          {
              new UserAccount
              {
                  ID = 1,
                  Username = "User"
              }
          }.AsQueryable();
          var mockSet = new Mock&lt;DbSet&lt;UserAccount&gt;&gt;();
          mockSet.As&lt;IDbAsyncEnumerable&lt;UserAccount&gt;&gt;().
              Setup(m =&gt; m.GetAsyncEnumerator()).
              Returns(new TestDbAsyncEnumerator&lt;UserAccount&gt;(userAccount.GetEnumerator()));
          mockSet.As&lt;IQueryable&lt;UserAccount&gt;&gt;()
              .Setup(m =&gt; m.Provider)
              .Returns(new TestDbAsyncQueryProvider&lt;UserAccount&gt;(userAccount.Provider));
          mockSet.As&lt;IQueryable&lt;UserAccount&gt;&gt;().Setup(m =&gt; m.Expression).Returns(userAccount.Expression);
          mockSet.As&lt;IQueryable&lt;UserAccount&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(userAccount.ElementType);
          mockSet.As&lt;IQueryable&lt;UserAccount&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(userAccount.GetEnumerator());
  
          AutoMapConfiguration.Configure();
          var entityService = new Mock&lt;IEntityService&lt;UserAccount&gt;&gt;();
  
          entityService
              .Setup(service =&gt; service.GetFirstOrDefaultAsync(It.IsAny&lt;Expression&lt;Func&lt;UserAccount, bool&gt;&gt;&gt;()))
              .ReturnsAsync(
                  (Expression&lt;Func&lt;UserAccount, bool&gt;&gt; predicate) =&gt; userAccount.FirstOrDefault(predicate)
              );
  
          var loginService = new LoginService(entityService.Object);
  
          // Act
          var result = await ((ILoginService)loginService).GetLoginDetailAsync("User");
  
          // Assert
          Assert.IsNotNull(result);
      }
  </code></pre>
  
  <p>This test should pass, as it should query on the <code>userAccount</code> but it fails, when I was debugging, and it went inside the <code>LoginService</code>, and <em>I checked <code>_entityService.Get().ToList()</code> it says 0 count, while it should really say count 1, the <code>userAccount</code> that I have setup</em>. 
  Afaik, the <code>IDbSet</code> is still not setup, and that's why the count is 0, and it's not returning true. How do I setup that? If it is correct, then why is this test failing? Also, I know that <code>moq</code> isn't really good for testing expression, but I got this <code>predicate</code> part of code from <a href="https://stackoverflow.com/a/5196875/710925">here</a>.</p>