MockQueryable.Moq 8.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package MockQueryable.Moq --version 8.0.1
                    
NuGet\Install-Package MockQueryable.Moq -Version 8.0.1
                    
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.Moq" Version="8.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MockQueryable.Moq" Version="8.0.1" />
                    
Directory.Packages.props
<PackageReference Include="MockQueryable.Moq" />
                    
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.Moq --version 8.0.1
                    
#r "nuget: MockQueryable.Moq, 8.0.1"
                    
#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.Moq@8.0.1
                    
#: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.Moq&version=8.0.1
                    
Install as a Cake Addin
#tool nuget:?package=MockQueryable.Moq&version=8.0.1
                    
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 net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  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 (8)

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

Package Downloads
Zeesfam.NetLib

Package Description

Franz.Common.Testing

Shared utility library for the Franz Framework.

Simpository

A simple database repository library

VG.Infrastructure.EntityFramework.UnitTest

Package Description

TNArch.UnitTestSpecification.Core

Package Description

GitHub repositories (11)

Showing the top 11 popular GitHub repositories that depend on MockQueryable.Moq:

Repository Stars
simplcommerce/SimplCommerce
A simple, cross platform, modulith ecommerce system built on .NET
Ombi-app/Ombi
Want a Movie or TV Show on Plex/Emby/Jellyfin? Use Ombi!
VirtoCommerce/vc-platform
Virto Commerce B2B Innovation Platform
CodeMazeBlog/CodeMazeGuides
The main repository for all the Code Maze guides
lkurzyniec/netcore-boilerplate
Boilerplate of API in .NET 9
Librum-Reader/Librum-Server
The Librum server
carlreid/StreamMaster
A powerful M3U proxy and stream management platform for Plex DVR, Emby, Jellyfin Live TV (and more)
Letterbook/Letterbook
Sustainable federated social media built for open correspondence
SapiensAnatis/Dawnshard
Server emulator for Dragalia Lost
matt-bentley/CleanArchitecture
An opinionated ASP.NET Core solution setup for creating web applications using Clean Architecture and Domain-Driven Design principles.
microsoftgraph/group-membership-management
Group Membership Management (GMM) is a service that dynamically manages the membership of AAD Groups. Groups managed by GMM can have their membership defined using existing AAD Groups and/or custom membership sources.
Version Downloads Last Updated
9.0.0 45,573 10/6/2025
8.0.1 21,409 10/6/2025
8.0.0 218,906 7/27/2025
7.0.4-beta 92,802 9/24/2024
7.0.3 4,432,741 9/2/2024
7.0.2 164,422 8/20/2024
7.0.1 2,239,987 3/7/2024
7.0.0 4,098,984 11/21/2022
6.0.1 4,576,714 3/28/2022
6.0.0 230,004 3/24/2022
5.0.2 305,102 3/24/2022
5.0.1 2,635,986 5/25/2021
5.0.0 1,249,243 11/12/2020
5.0.0-preview.7 33,251 7/28/2020
3.1.3 2,138,010 5/19/2020
3.1.2 143,754 4/17/2020
3.1.1 403,078 1/25/2020
3.0.2 44,947 12/20/2019
3.0.1 46,882 10/11/2019
3.0.0 15,146 9/30/2019
1.1.0 690,844 2/28/2019
1.0.4 48,801 1/29/2019
1.0.3 311,373 8/30/2018
1.0.2 91,617 1/17/2018
1.0.1 9,642 12/26/2017
1.0.0 20,605 12/26/2017

#88  MockQueryable.NSubstitute 8.0.0 Breaks .AsQueryable() calls on DbSet - Thanks @Catlandor