/MockQueryable

Moking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc

Primary LanguageC#

MockQueryable

Build status Build Status Downloads Downloads

Extensions for mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc. by Moq or NSubstitute 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.

How do I get started?

//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);

Check out the sample project

Where can I get it?

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

Can I use it with my favorite mock framework?

You can install MockQueryable.Core from the package manager console:

PM> Install-Package MockQueryable.Core

Downloads

Then make own extension for your favorite mock framework