Moq.EntityFrameworkCore 10.0.0.2

Moq.EntityFrameworkCore

Build and Test Downloads

This library helps you mocking EntityFramework contexts. Now you will be able to test methods that are using DbSet<TEntity> or DbQuery<TEntity> from DbContext in an effective way.

Installation - NuGet Packages

Install-Package Moq.EntityFrameworkCore

Usage

For example we can assume that we have the following production code:

public class UsersContext : DbContext
{
    public virtual DbSet<User> Users { get; set; }
}

To mock Users and Roles you only need to implement the following 3 steps:

1. Create DbContext mock:

var userContextMock = new Mock<UsersContext>();

2. Generate your entities:

IList<User> users = ...;

3. Setup DbSet or DbQuery property:

userContextMock.Setup(x => x.Users).ReturnsDbSet(users);

or

userContextMock.SetupGet(x => x.Users).ReturnsDbSet(users);

or

userContextMock.SetupSequence(x => x.Set<User>())
  .ReturnsDbSet(new List<User>())
  .ReturnsDbSet(users);

And this is all. You can use your DbContext in your tests.

The second option is mocking DbSet that is part of the interface:

public interface IBlogContext
{
   DbSet<Post> Posts { get; }
}

And then use:

var posts = new List<Post>();
var contextMock = new Mock<IBlogContext>();
contextMock.Setup(p => p.Posts).ReturnsDbSet(posts);

Using ReturnsDbSetWithGlobalFilter

You can also use ReturnsDbSetWithGlobalFilter to set up a DbSet with a global filter. For example:

var users = new List<User> { new User { IsActive = true }, new User { IsActive = false } };
var userContextMock = new Mock<UsersContext>();
userContextMock.Setup(x => x.Users).ReturnsDbSetWithGlobalFilter(users, u => u.IsActive);

New Usage Option: ReturnsDbSetDynamic

You can now use the ReturnsDbSetDynamic method for scenarios where you need lazy evaluation of your DbSet. This ensures that the DbSet is re-evaluated dynamically at runtime.

userContextMock.Setup(x => x.Users).ReturnsDbSetDynamic(users);

Explanation of Differences

  • ReturnsDbSet: Statically resolves the DbSet value during setup.
  • ReturnsDbSetDynamic: Dynamically resolves the DbSet value using a lambda, ensuring it reflects changes at runtime.

You will find examples of this library in the repository.

No packages depend on Moq.EntityFrameworkCore.

Version 10.0.0.0

  • Updated to .NET 10.0
  • Updated to Entity Framework Core 10.0
  • Updated async enumerator mock to use It.IsAny()
  • Added comprehensive NuGet package configuration
  • Added SourceLink support for debugging
  • Enhanced package metadata for better discoverability
  • Added symbol package generation (.snupkg)
  • Improved build configuration for CI/CD
  • Added .editorconfig for consistent code formatting
  • Added Directory.Build.props for shared build properties

.NET 10.0

Version Downloads Last updated
10.0.0.2 1 2026-05-23
10.0.0 0 2025-12-03
9.0.0.10 0 2025-12-03
9.0.0.5 0 2025-04-27
9.0.0.1 0 2025-01-10
8.0.1.7 0 2025-01-10
8.0.1.6 0 2025-01-10
8.0.1.2 0 2024-01-27
8.0.1.1 0 2024-01-27
8.0.0.1 0 2024-01-27
7.0.0.2 0 2022-12-01
6.0.1.4 0 2022-05-29
6.0.1.3 0 2022-05-29
6.0.1.2 0 2022-01-25
6.0.0.6 0 2022-01-25
5.0.0.2 0 2021-03-12
5.0.0.1 0 2020-11-29
3.1.2.13 0 2020-11-29
3.1.2.6 0 2020-06-29
3.1.2.1 0 2020-02-26
3.0.0.10 0 2019-11-09
3.0.0.4 0 2019-10-06
2.2.1.1 0 2020-06-30
2.2.1 0 2019-02-01
2.0.1 0 2018-02-23
1.0.0 0 2017-10-13