title: How to Mock Asynchronous Methods in JustMock?
body: <p>I am new in Mocking. I am trying to mock my dbContext using JustMock. I am using Entity Framework 6. In Entity Framework 6 some features are asynchronous. I am successfully mock synchronous methods and my test is successfully pass. But I am stuck in asynchronous methods.
  My test still pass after i put wrong Assert.</p>
  
  <p>Here is my Code :</p>
  
  <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 void Add(T entity)
      {
          InnerDbSet.Add(entity);
      }
  
      public virtual Task&lt;T&gt; FindAsync(long id)
      {
          return InnerDbSet.FirstOrDefaultAsync(x =&gt; x.Id == id &amp;&amp; !x.IsDelete);
      }
  }
  </code></pre>
  
  <p>DbContext :</p>
  
  <pre><code>public class TimeSketchContext : DbContext
  {
      public DbSet&lt;EmployeeSkill&gt; EmployeeSkill { get; set; }
  }
  </code></pre>
  
  <p>Test :</p>
  
  <pre><code>public class BaseRepositoryTest
  {
      readonly TimeSketchContext _mockDataContext = Mock.Create&lt;TimeSketchContext&gt;();
      private  BaseRepository&lt;EmployeeSkill&gt; _repository;
      public BaseRepositoryTest()
      {
          _repository = new BaseRepository&lt;EmployeeSkill&gt;(_mockDataContext);
      }
  
      [Fact]
      public void Add_Should_Call_Once()
      {
          _repository.Add(Arg.IsAny&lt;EmployeeSkill&gt;());
          Mock.Assert(() =&gt; _mockDataContext.Set&lt;EmployeeSkill&gt;().Add(Arg.IsAny&lt;EmployeeSkill&gt;()), Occurs.Once());
      }
  
      [Fact]
      public async void Find_Should_Call_Once()
      {
          await _repository.FindAsync(Arg.AnyInt);
          Mock.Assert(() =&gt; _mockDataContext.Set&lt;EmployeeSkill&gt;().LastOrDefault(x =&gt; x.Id == Arg.AnyInt &amp;&amp; !x.IsDelete), Occurs.Once());
          // This Test Will be fail Because my _repository.FindAsync is call FirstOrDefaultAsync. But it is pass.
      }
  }
  </code></pre>
  
  <p>I am using :</p>
  
  <ul>
  <li>Visual Studio 2013 Ultimate </li>
  <li>.NET 4.5.1  </li>
  <li>JustMock Q3 2013 (2013.3.1015.0)</li>
  <li>XUnit</li>
  </ul>