MockQueryable.Core 8.0.1
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 | Install-Package MockQueryable.Core |
||
MockQueryable.EntityFrameworkCore | Install-Package MockQueryable.EntityFrameworkCore |
||
MockQueryable.Moq | Install-Package MockQueryable.Moq |
||
MockQueryable.NSubstitute | Install-Package MockQueryable.NSubstitute |
||
MockQueryable.FakeItEasy | Install-Package MockQueryable.FakeItEasy |
✅ Build & Status
⭐ GitHub Stats
💡 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.
Showing the top 20 packages that depend on MockQueryable.Core.
Packages | Downloads |
---|---|
MockQueryable.EntityFrameworkCore
Extension for mocking Entity Framework Core 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.
|
22 |
MockQueryable.EntityFrameworkCore
Extension for mocking Entity Framework Core 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.
|
21 |
MockQueryable.EntityFrameworkCore
Extension for mocking Entity Framework Core 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.
|
20 |
MockQueryable.EntityFrameworkCore
Extension for mocking Entity Framework Core 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.
|
19 |
MockQueryable.EntityFrameworkCore
Extension for mocking Entity Framework Core 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.
|
18 |
MockQueryable.EntityFrameworkCore
Extension for mocking Entity Framework Core 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.
|
16 |
MockQueryable.EntityFrameworkCore
Extension for mocking Entity Framework Core 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.
|
15 |
MockQueryable.EntityFrameworkCore
Extension for mocking Entity Framework Core 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.
|
14 |
MockQueryable.EntityFrameworkCore
Extension for mocking Entity Framework Core 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.
|
8 |
MockQueryable.EntityFrameworkCore
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.
|
2 |
#88 Fix .AsQueryable calls to the test data set - Thanks @Catlandor
#91 Move EF Dependency to v8 - Thanks @johnw86
.NET 8.0
- No dependencies.
Version | Downloads | Last updated |
---|---|---|
9.0.0 | 2 | 10/09/2025 |
8.0.1 | 2 | 10/10/2025 |
8.0.0 | 8 | 08/09/2025 |
7.0.4-beta | 17 | 10/06/2024 |
7.0.3 | 17 | 10/07/2024 |
7.0.2 | 16 | 10/07/2024 |
7.0.1 | 14 | 10/07/2024 |
7.0.0 | 17 | 10/07/2024 |
6.0.1 | 17 | 10/07/2024 |
6.0.0 | 17 | 10/07/2024 |
5.0.2 | 14 | 10/07/2024 |
5.0.1 | 18 | 02/06/2022 |
5.0.0 | 15 | 10/07/2024 |
5.0.0-preview.7 | 19 | 10/07/2024 |
3.1.3 | 17 | 10/07/2024 |
3.1.2 | 17 | 10/07/2024 |
3.1.1 | 18 | 10/07/2024 |
3.0.2 | 15 | 10/07/2024 |
3.0.1 | 13 | 10/07/2024 |
3.0.0 | 18 | 10/07/2024 |
1.1.0 | 18 | 10/07/2024 |
1.0.4 | 14 | 10/07/2024 |
1.0.3 | 17 | 10/07/2024 |
1.0.2 | 17 | 10/07/2024 |
1.0.1 | 15 | 10/07/2024 |
1.0.0 | 17 | 10/07/2024 |