EF Core MoqQueryable Setup Repository

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

With MockQueryable.EntityFrameworkCore, you can use any of the following packages.

So let's install the following packages from the Package Manager Console.

PM> Install-Package MockQueryable.Moq
PM> Install-Package MockQueryable.NSubstitute
PM> Install-Package MockQueryable.FakeItEasy

Once these packages are installed, first we need to create an IAuthorRepository interface.

public interface IAuthorRepository
{
    IQueryable<Author> GetQueryable();

    void CreateAuthor(Author author);

    List<Author> GetAll();
}

It contains three simple methods, and we need to implement these methods in a class as shown below.

public class TestDbSetRepository : IAuthorRepository
{
    private readonly DbSet<Author> _dbSet;

    public TestDbSetRepository(DbSet<Author> dbSet)
    {
        _dbSet = dbSet;
    }
    public IQueryable<Author> GetQueryable()
    {
        return _dbSet;
    }

    public void CreateAuthor(Author author)
    {
        _dbSet.Add(author);
    }

    public List<Author> GetAll()
    {
        return _dbSet.AsQueryable().ToList();
    }
}


Got any EF Core MoqQueryable Question?