MockQueryable.Moq 9.0.0

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

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

✅ Build & Status

.NET Core AppVeyor 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.

🚀 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.


No packages depend on MockQueryable.Moq.

Upgrade to .Net 9 and EntityFrameworkCore 9

.NET 9.0

Version Downloads Last updated
11.0.0 3 11/19/2026
10.0.13 3 11/01/2026
10.0.12 11 09/14/2026
10.0.8 33 06/10/2026
10.0.5 37 03/21/2026
10.0.2 26 02/06/2026
10.0.1 22 11/25/2025
10.0.0 37 11/25/2025
9.0.0 31 10/16/2025
8.0.1 34 10/16/2025
8.0.0 39 09/14/2025
7.0.4-beta 39 10/05/2024
7.0.3 36 11/30/2025
7.0.2 37 10/04/2024
7.0.1 51 03/13/2024
7.0.0 55 01/09/2024
6.0.1 21 01/08/2024
6.0.0 19 01/08/2024
5.0.2 482 12/11/2023
5.0.1 16 01/08/2024
5.0.0 27 01/08/2024
5.0.0-preview.7 23 01/09/2024
3.1.3 14 01/09/2024
3.1.2 28 01/08/2024
3.1.1 32 01/08/2024
3.0.2 31 01/09/2024
3.0.1 16 01/08/2024
3.0.0 14 01/08/2024
1.1.0 34 01/08/2024
1.0.4 22 01/09/2024
1.0.3 37 01/09/2024
1.0.2 15 01/09/2024
1.0.1 37 01/08/2024
1.0.0 38 01/08/2024