Skip to main content
net10.0

Core package for MockQueryable extensions for mocking operations such ToListAsync, FirstOrDefaultAsync etc. When writing tests for your application it is often desirable to avoid hitting the database. The extension allows you to achieve this by creating a context – with behavior defined by your tests – that makes use of in-memory data.

Published
Sep 13, 2026
1mo ago
Total Downloads
51.2M
51,214,281

Install

dotnet add package MockQueryable.Core --version 10.0.12
ProductVersions
.NET
net10.0

README

MockQueryable

Extensions for mocking Entity Framework Core async queries like ToListAsync, FirstOrDefaultAsync, and more using popular mocking libraries such as Moq, NSubstitute, and FakeItEasy β€” all without hitting the database.

❀️ If you really like the tool, please πŸ‘‰ Support the project or β˜• Buy me a coffee.


πŸ“¦ NuGet Packages: Total Downloads

Package Latest Version Install via Package Manager
Download Version Install-Package MockQueryable.Core
Download Version Install-Package MockQueryable.EntityFrameworkCore
Download Version Install-Package MockQueryable.Moq
Download Version Install-Package MockQueryable.NSubstitute
Download Version Install-Package MockQueryable.FakeItEasy

βœ… Build & Status

codecov build License


⭐ GitHub Stats

Stars Contributors Last Commit Commit Activity Open Issues


πŸ’‘ Why Use MockQueryable?

Avoid hitting the real database in unit tests when querying via IQueryable:

var query = _userRepository.GetQueryable();

await query.AnyAsync(x => ...);
await query.FirstOrDefaultAsync(x => ...);
await query.ToListAsync();
// etc.

How It Works

MockQueryable creates a custom implementation of:

  • IQueryable<T>
  • IAsyncEnumerable<T>
  • IAsyncQueryProvider

allowing EF Core async extensions to execute against in-memory collections.

πŸ‘·πŸ»β€See Architecture.


πŸš€ Getting Started

1. Create Test Data

var users = new List<UserEntity>
{
    new UserEntity { LastName = "Smith", DateOfBirth = new DateTime(2012, 1, 20) },
    // More test data...
};

2. Build the Mock

var mock = users.BuildMock(); // for IQueryable

3. Set Up in Your favorite Mocking Framework

Moq
_userRepository.Setup(x => x.GetQueryable()).Returns(mock);
NSubstitute
_userRepository.GetQueryable().Returns(mock);
FakeItEasy
A.CallTo(() => userRepository.GetQueryable()).Returns(mock);

πŸ—ƒοΈ Mocking DbSet<T>

var mockDbSet = users.BuildMockDbSet();

// Moq
var repo = new TestDbSetRepository(mockDbSet.Object);

// NSubstitute / FakeItEasy
var repo = new TestDbSetRepository(mockDbSet);

πŸ”§ Adding Custom Logic

Example: Custom FindAsync

mock.Setup(x => x.FindAsync(userId)).ReturnsAsync((object[] ids) =>
{
    var id = (Guid)ids[0];
    return users.FirstOrDefault(x => x.Id == id);
});

Example: Custom Expression Visitor

Build a mock with the custom SampleLikeExpressionVisitor for testing EF.Functions.Like

var mockDbSet = users.BuildMockDbSet<UserEntity, SampleLikeExpressionVisitor>();

🧩 Extend for Other Frameworks

You can even create your own extensions. Check the example here.


πŸ” Sample Project

See the sample project for working examples.


Changes from Previous Version

No structural changes between v10.0.8 and v10.0.12.

Adoption Assessment

Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.

No SPDX license expression is published. Review the license text and your organisation's policy before redistribution.Compatibility metadata lists 1 target framework; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.