An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# Moq.EntityFrameworkCore
[![Build Status](https://dev.azure.com/OpenSource-jankowskimichalpl/Moq.EntityFrameworkCore/_apis/build/status/MichalJankowskii.Moq.EntityFrameworkCore?branchName=master)](https://dev.azure.com/OpenSource-jankowskimichalpl/Moq.EntityFrameworkCore/_build/latest?definitionId=1&branchName=master)
[![Downloads](https://img.shields.io/nuget/dt/Moq.EntityFrameworkCore.svg)](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).