MockQueryable.EntityFrameworkCore 9.0.0

dotnet add package MockQueryable.EntityFrameworkCore --version 9.0.0
                    
NuGet\Install-Package MockQueryable.EntityFrameworkCore -Version 9.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MockQueryable.EntityFrameworkCore" Version="9.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MockQueryable.EntityFrameworkCore" Version="9.0.0" />
                    
Directory.Packages.props
<PackageReference Include="MockQueryable.EntityFrameworkCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MockQueryable.EntityFrameworkCore --version 9.0.0
                    
#r "nuget: MockQueryable.EntityFrameworkCore, 9.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MockQueryable.EntityFrameworkCore@9.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MockQueryable.EntityFrameworkCore&version=9.0.0
                    
Install as a Cake Addin
#tool nuget:?package=MockQueryable.EntityFrameworkCore&version=9.0.0
                    
Install as a Cake Tool

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.


Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (7)

Showing the top 5 NuGet packages that depend on MockQueryable.EntityFrameworkCore:

Package Downloads
MockQueryable.Moq

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.

MockQueryable.NSubstitute

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.

MockQueryable.FakeItEasy

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.

Havit.Data.EntityFrameworkCore.Patterns

HAVIT .NET Framework Extensions - Entity Framework Core Extensions - Data Patterns

RESTworld.Business

Package Description

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on MockQueryable.EntityFrameworkCore:

Repository Stars
nuyonu/N-Tier-Architecture
This is a n-layer architecture based on Common web application architectures.
SapiensAnatis/Dawnshard
Server emulator for Dragalia Lost
Version Downloads Last Updated
9.0.0 15,413 10/6/2025
8.0.1 8,014 10/6/2025
8.0.0 264,855 7/27/2025
7.0.4-beta 131,862 9/24/2024
7.0.3 5,487,118 9/2/2024
7.0.2 247,521 8/20/2024
7.0.1 3,070,968 3/7/2024
7.0.0 5,287,224 11/21/2022
6.0.1 5,716,060 3/28/2022
6.0.0 363,490 3/24/2022
5.0.2 374,579 3/24/2022
5.0.1 3,481,638 5/25/2021
5.0.0 1,396,154 11/12/2020
5.0.0-preview.7 33,673 7/28/2020
3.1.3 2,417,559 5/19/2020

Upgrade to .Net 9 and EntityFrameworkCore 9