https://github.com/MichalJankowskii/Moq.EntityFrameworkCore
Library that provides methods that will help you with mocking EntityFrameworkCore.
https://github.com/MichalJankowskii/Moq.EntityFrameworkCore
Last synced: 8 months ago
JSON representation
Library that provides methods that will help you with mocking EntityFrameworkCore.
- Host: GitHub
- URL: https://github.com/MichalJankowskii/Moq.EntityFrameworkCore
- Owner: MichalJankowskii
- License: mit
- Created: 2017-10-04T19:05:27.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-05-05T10:15:32.000Z (over 1 year ago)
- Last Synced: 2024-11-07T09:12:37.616Z (about 1 year ago)
- Language: C#
- Size: 2.67 MB
- Stars: 253
- Watchers: 10
- Forks: 27
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: Changelog.txt
- License: LICENSE
Awesome Lists containing this project
- awesome-entity-framework-core - Moq.EntityFrameworkCore - This library helps you with mocking EntityFramework contexts. (Supported Packages / Contents)
README
# Moq.EntityFrameworkCore
[](https://dev.azure.com/OpenSource-jankowskimichalpl/Moq.EntityFrameworkCore/_build/latest?definitionId=1&branchName=master)
[](https://www.nuget.org/packages/Moq.EntityFrameworkCore/)
This library helps you mocking EntityFramework contexts. Now you will be able to test methods that are using `DbSet` or `DbQuery` 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 Users { get; set; }
}
```
To mock Users and Roles you only need to implement the following 3 steps:
1\. Create `DbContext` mock:
```csharp
var userContextMock = new Mock();
```
2\. Generate your entities:
```csharp
IList users = ...;
```
3\. Setup `DbSet` or `DbQuery` property:
```csharp
userContextMock.Setup(x => x.Users).ReturnsDbSet(users);
```
or
```csharp
userContextMock.SetupGet(x => x.Users).ReturnsDbSet(users);
```
or
```csharp
userContextMock.SetupSequence(x => x.Set())
.ReturnsDbSet(new List())
.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:
```csharp
public interface IBlogContext
{
DbSet Posts { get; }
}
```
And then use:
```csharp
var posts = new List();
var contextMock = new Mock();
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:
```csharp
var users = new List { new User { IsActive = true }, new User { IsActive = false } };
var userContextMock = new Mock();
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.
```csharp
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](https://github.com/MichalJankowskii/Moq.EntityFrameworkCore/blob/master/src/Moq.EntityFrameworkCore.Examples/UsersServiceTest.cs).