Library that provides methods that will help you with mocking EntityFrameworkCore.
Switch branches/tags
Clone or download

README.md

Moq.EntityFrameworkCore

Build Status Downloads

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

Installation - NuGet Packages

Install-Package Moq.EntityFrameworkCore

Usage

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

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

For mocking Users you need only implement following 3 steps:

1. Create DbContext mock:

var userContextMock = new Mock<UsersContext>();

2. Generate your entities:

IList<User> users = ...;

3. Setup DbSet propery:

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

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

You will find examples of this library in repository.