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.
Install-Package Moq.EntityFrameworkCore
For example we can assume that we have the following production code:
public class UsersContext : DbContext
{
public virtual DbSet<User> Users { get; set; }
public virtual DbQuery<Role> Roles { 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 = ...;
IList<Roles> roles = ...;3. Setup DbSet or DbQuery property:
userContextMock.Setup(x => x.Users).ReturnsDbSet(users);
userContextMock.Setup(x => x.Roles).ReturnsDbQuery(roles);And this is all. You can use your DbContext in your tests.
You will find examples of this library in the repository.