Extensions for mocking Entity Framework Core (EFCore) operations such ToListAsync, FirstOrDefaultAsync etc. by Moq, NSubstitute or FakeItEasy When writing tests for your application it is often desirable to avoid hitting the database. The extensions allow you to achieve this by creating a context – with behavior defined by your tests – that makes use of in-memory data.
If you have something similar to the following code:
var query = _userRepository.GetQueryable();
await query.AnyAsync(x =>...)
await query.FirstOrDefaultAsync(x =>...)
query.CountAsync(x => ...)
query.ToListAsync()
//etc.
and you want to cover it by unit tests
//1 - create a List<T> with test items
var users = new List<UserEntity>()
{
new UserEntity{LastName = "ExistLastName", DateOfBirth = DateTime.Parse("01/20/2012")},
...
};
//2 - build mock by extension
var mock = users.AsQueryable().BuildMock();
//3 - setup the mock as Queryable for Moq
_userRepository.Setup(x => x.GetQueryable()).Returns(mock.Object);
//3 - setup the mock as Queryable for NSubstitute
_userRepository.GetQueryable().Returns(mock);
//3 - setup the mock as Queryable for FakeItEasy
A.CallTo(() => userRepository.GetQueryable()).Returns(mock);
Do you prefer DbSet?
//2 - build mock by extension
var mock = users.AsQueryable().BuildMockDbSet();
//3 - setup DbSet for Moq
var userRepository = new TestDbSetRepository(mock.Object);
//3 - setup DbSet for NSubstitute or FakeItEasy
var userRepository = new TestDbSetRepository(mock);
Do you use DbQuery?
//2 - build mock by extension
var mock = users.AsQueryable().BuildMockDbQuery();
//3 - setup the mock as Queryable for Moq
_userRepository.Setup(x => x.GetQueryable()).Returns(mock.Object);
//3 - setup the mock as Queryable for NSubstitute
_userRepository.GetQueryable().Returns(mock);
//3 - setup the mock as Queryable for FakeItEasy
A.CallTo(() => userRepository.GetQueryable()).Returns(mock);
MockQueryable creates for your tests a mock object based on in-memory data, but you can also add some custome logic to it.
var userId = Guid.NewGuid();
var users = new List<UserEntity>
{
new UserEntity{Id = userId,LastName = "ExistLastName", DateOfBirth = DateTime.Parse("01/20/2012")},
//etc.
};
var mock = users.AsQueryable().BuildMockDbSet();
//Aditional setup for FindAsync
mock.Setup(x => x.FindAsync(userId)).ReturnsAsync((object[] ids) =>
{
var id = (Guid)ids[0];
return users.FirstOrDefault(x => x.Id == id);
});
var userRepository = new TestDbSetRepository(mock.Object);
//Execution FindAsync
var user = await ((DbSet<UserEntity>) userRepository.GetQueryable()).FindAsync(userId);
Check out the sample project
First, install NuGet.
If you are using Moq - then, install MockQueryable.Moq from the package manager console:
PM> Install-Package MockQueryable.Moq
If you are using NSubstitute - then, install MockQueryable.NSubstitute from the package manager console:
PM> Install-Package MockQueryable.NSubstitute
If you are using FakeItEasy - then, install MockQueryable.FakeItEasy from the package manager console:
PM> Install-Package MockQueryable.FakeItEasy
You can install MockQueryable.EntityFrameworkCore from the package manager console:
PM> Install-Package MockQueryable.EntityFrameworkCore
or even MockQueryable.Core
PM> Install-Package MockQueryable.Core
Then make your own extension for your favorite mock framework