title: How to mock Entity Framework 6 Async methods?
body: <p>I am new in mocking. I want to mock up my base repository which is depend on Entity Framework 6 DbContext But I fail. I searched in Google a lot but did not get any sufficient result. At last I got an example at <a href="http://msdn.microsoft.com/en-us/data/dn314429#async">testing with async queries</a> and try to follow but it is worked for me.</p>
  
  <p>Here is my code :</p>
  
  <p>DbContext :</p>
  
  <pre><code>public class TimeSketchContext : DbContext
  {
      public virtual DbSet&lt;EmployeeSkill&gt; EmployeeSkill { get; set; }
  }
  </code></pre>
  
  <p>Base Repository :</p>
  
  <pre><code>public class BaseRepository&lt;T&gt; : IRepositoryBase&lt;T&gt; where T : class, IEntity, new()
  {
      protected readonly DbContext InnerDbContext;
      protected DbSet&lt;T&gt; InnerDbSet;
  
      public BaseRepository(DbContext innerDbContext)
      {
          InnerDbContext = innerDbContext;
          InnerDbSet = InnerDbContext.Set&lt;T&gt;();
      }
  
      public virtual Task&lt;T&gt; FindAsync(long id)
      {
          return InnerDbSet.FirstOrDefaultAsync(x=&gt;x.Id == id);
      }
  </code></pre>
  
  <p>} </p>
  
  <p>Test :</p>
  
  <pre><code>    [Fact]
      public async Task DbTest()
      {
          var dummyData = GetEmployeeSkills();
          var mockSet = new Mock&lt;DbSet&lt;EmployeeSkill&gt;&gt;();
  
          mockSet.As&lt;IDbAsyncEnumerable&lt;EmployeeSkill&gt;&gt;()
              .Setup(x =&gt; x.GetAsyncEnumerator())
              .Returns(new TestDbAsyncEnumerator&lt;EmployeeSkill&gt;(dummyData.GetEnumerator()));
  
          mockSet.As&lt;IQueryable&lt;EmployeeSkill&gt;&gt;()
              .Setup(x =&gt; x.Provider)
              .Returns(new TestDbAsyncQueryProvider&lt;EmployeeSkill&gt;(dummyData.Provider));
  
          mockSet.As&lt;IQueryable&lt;EmployeeSkill&gt;&gt;().Setup(m =&gt; m.Expression).Returns(dummyData.Expression);
          mockSet.As&lt;IQueryable&lt;EmployeeSkill&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(dummyData.ElementType);
          mockSet.As&lt;IQueryable&lt;EmployeeSkill&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(dummyData.GetEnumerator());
  
          var mockContext = new Mock&lt;TimeSketchContext&gt;();
          mockContext.Setup(c =&gt; c.EmployeeSkill).Returns(mockSet.Object);
  
          var baseRepository = new BaseRepository&lt;EmployeeSkill&gt;(mockContext.Object);
  
          var data = await baseRepository.FindAsync(1);
  
          Assert.NotEqual(null, data);
  
      }
  
      private EmployeeSkill GetEmployeeSkill()
      {
          return new EmployeeSkill
          {
              SkillDescription = "SkillDescription",
              SkillName = "SkillName",
              Id = 1
          };
      }
  
      private IQueryable&lt;EmployeeSkill&gt; GetEmployeeSkills()
      {
          return new List&lt;EmployeeSkill&gt;
          {
              GetEmployeeSkill(),
              GetEmployeeSkill(),
              GetEmployeeSkill(),
          }.AsQueryable();
      }
  </code></pre>
  
  <p>Result is : </p>
  
  <blockquote>
    <p>Assert.NotEqual() Failure</p>
  </blockquote>
  
  <p>I think problem is </p>
  
  <pre><code> public BaseRepository(DbContext innerDbContext)
   {
       InnerDbContext = innerDbContext;
       InnerDbSet = InnerDbContext.Set&lt;T&gt;();  &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
   }
  </code></pre>
  
  <p>But don`t understand why and how to solve this.</p>
  
  <p>I am using :</p>
  
  <ul>
  <li>Visual Studio 2013 Ultimate</li>
  <li>Moq</li>
  <li>xUnit</li>
  </ul>
  
  <p>Thank`s in advance.</p>