Moq.EntityFrameworkCore
8.0.1.2
dotnet add package Moq.EntityFrameworkCore --version 8.0.1.2
NuGet\Install-Package Moq.EntityFrameworkCore -Version 8.0.1.2
<PackageReference Include="Moq.EntityFrameworkCore" Version="8.0.1.2" />
paket add Moq.EntityFrameworkCore --version 8.0.1.2
#r "nuget: Moq.EntityFrameworkCore, 8.0.1.2"
// Install Moq.EntityFrameworkCore as a Cake Addin #addin nuget:?package=Moq.EntityFrameworkCore&version=8.0.1.2 // Install Moq.EntityFrameworkCore as a Cake Tool #tool nuget:?package=Moq.EntityFrameworkCore&version=8.0.1.2
Moq.EntityFrameworkCore
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);
You will find examples of this library in the repository.
Product | Versions 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. |
-
net8.0
- Microsoft.EntityFrameworkCore (>= 8.0.1)
- Moq (>= 4.20.70)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on Moq.EntityFrameworkCore:
Package | Downloads |
---|---|
Atacorp.ApiEssentials.Testing.XUnit
Package Description |
|
Devocean.Tests
Devocean.Tests is a set of base classes and helpers inteded to support implementing integration and unit tests on projects based on Devocean.Core |
|
Atacorp.TestingEssentials.XUnit
Package Description |
GitHub repositories (5)
Showing the top 5 popular GitHub repositories that depend on Moq.EntityFrameworkCore:
Repository | Stars |
---|---|
CodeMazeBlog/CodeMazeGuides
The main repository for all the Code Maze guides
|
|
TanvirArjel/EFCore.GenericRepository
This repository contains Generic Repository implementation for Entity Framework Core
|
|
matthewrenze/clean-architecture-core
A sample app for my online course "Clean Architecture: Patterns, Practices, and Principles" in .NET Core
|
|
pkuehnel/TeslaSolarCharger
A software to let your Tesla charge with solar energy ☀
|
|
SapiensAnatis/Dawnshard
Server emulator for Dragalia Lost
|
Version | Downloads | Last updated |
---|---|---|
8.0.1.2 | 1,479,536 | 1/27/2024 |
8.0.1.1 | 29,697 | 1/27/2024 |
8.0.0.1 | 14,448 | 1/27/2024 |
7.0.0.2 | 2,501,309 | 12/1/2022 |
6.0.1.4 | 1,880,052 | 5/29/2022 |
6.0.1.3 | 130,683 | 5/29/2022 |
6.0.1.2 | 466,268 | 1/25/2022 |
6.0.0.6 | 48,690 | 1/25/2022 |
5.0.0.2 | 629,806 | 3/12/2021 |
5.0.0.1 | 117,365 | 11/29/2020 |
3.1.2.13 | 316,677 | 11/29/2020 |
3.1.2.6 | 174,876 | 6/29/2020 |
3.1.2.1 | 90,523 | 2/26/2020 |
3.0.0.10 | 42,435 | 11/9/2019 |
3.0.0.4 | 10,270 | 10/6/2019 |
2.2.1.1 | 140,734 | 6/30/2020 |
2.2.1 | 163,265 | 2/1/2019 |
2.0.1 | 195,972 | 2/23/2018 |
1.0.0 | 12,721 | 10/13/2017 |
Version 8.0.0.1
Added support for EntityFrameworkCore 8.0