How to mock an async repository with Entity Framework Core
<p>I'm trying to create a unit test for a class that calls into an async repository.  I'm using ASP.NET Core and Entity Framework Core.  My generic repository looks like this.</p>

<pre><code>public class EntityRepository&lt;TEntity&gt; : IEntityRepository&lt;TEntity&gt; where TEntity : class
{
    private readonly SaasDispatcherDbContext _dbContext;
    private readonly DbSet&lt;TEntity&gt; _dbSet;

    public EntityRepository(SaasDispatcherDbContext dbContext)
    {
        _dbContext = dbContext;
        _dbSet = dbContext.Set&lt;TEntity&gt;();
    }

    public virtual IQueryable&lt;TEntity&gt; GetAll()
    {
        return _dbSet;
    }

    public virtual async Task&lt;TEntity&gt; FindByIdAsync(int id)
    {
        return await _dbSet.FindAsync(id);
    }

    public virtual IQueryable&lt;TEntity&gt; FindBy(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate)
    {
        return _dbSet.Where(predicate);
    }

    public virtual void Add(TEntity entity)
    {
        _dbSet.Add(entity);
    }
    public virtual void Delete(TEntity entity)
    {
        _dbSet.Remove(entity);
    }

    public virtual void Update(TEntity entity)
    {
        _dbContext.Entry(entity).State = EntityState.Modified;
    }

    public virtual async Task SaveChangesAsync()
    {
        await _dbContext.SaveChangesAsync();
    }
}
</code></pre>

<p>Then I have a service class that calls FindBy and FirstOrDefaultAsync on an instance of the repository:</p>

<pre><code>    public async Task&lt;Uri&gt; GetCompanyProductURLAsync(Guid externalCompanyID, string productCode, Guid loginToken)
    {            
        CompanyProductUrl companyProductUrl = await _Repository.FindBy(u =&gt; u.Company.ExternalCompanyID == externalCompanyID &amp;&amp; u.Product.Code == productCode.Trim()).FirstOrDefaultAsync();

        if (companyProductUrl == null)
        {
            return null;
        }

        var builder = new UriBuilder(companyProductUrl.Url);
        builder.Query = $"-s{loginToken.ToString()}";

        return builder.Uri;
    }
</code></pre>

<p>I'm trying to mock the repository call in my test below:</p>

<pre><code>    [Fact]
    public async Task GetCompanyProductURLAsync_ReturnsNullForInvalidCompanyProduct()
    {
        var companyProducts = Enumerable.Empty&lt;CompanyProductUrl&gt;().AsQueryable();

        var mockRepository = new Mock&lt;IEntityRepository&lt;CompanyProductUrl&gt;&gt;();
        mockRepository.Setup(r =&gt; r.FindBy(It.IsAny&lt;Expression&lt;Func&lt;CompanyProductUrl, bool&gt;&gt;&gt;())).Returns(companyProducts);

        var service = new CompanyProductService(mockRepository.Object);

        var result = await service.GetCompanyProductURLAsync(Guid.NewGuid(), "wot", Guid.NewGuid());

        Assert.Null(result);
    }
</code></pre>

<p>However, when the test executes the call to the repository, I get the following error:</p>

<pre><code>The provider for the source IQueryable doesn't implement IAsyncQueryProvider. Only providers that implement IEntityQueryProvider can be used for Entity Framework asynchronous operations.
</code></pre>

<p>How can I properly mock the repository to get this to work?</p>