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.
Install-Package Moq.EntityFramework.Helpers
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).Returns(users);
And this is all. You can use your DbContext
in your tests.
You will find examples of this library in repository.