Moqing Entity Framework 6 .Include() using DbSet<>
Here is a complete example using Moq. You can paste the entire example into your unit test class. Thanks to comments by @jbaum012 and @Skuli. I also recommend the excellent tutorial from Microsoft.
// An Address entity
public class Address
{
public int Id { get; set; }
public string Line1 { get; set; }
}
// A Person referencing Address
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Address Address { get; set; }
}
// A DbContext with persons and devices
// Note use of virtual (see the tutorial reference)
public class PersonContext : DbContext
{
public virtual DbSet<Person> Persons { get; set; }
public virtual DbSet<Address> Addresses { get; set; }
}
// A simple class to test
// The dbcontext is injected into the controller
public class PersonsController
{
private readonly PersonContext _personContext;
public PersonsController(PersonContext personContext)
{
_personContext = personContext;
}
public IEnumerable<Person> GetPersons()
{
return _personContext.Persons.Include("Address").ToList();
}
}
// Test the controller above
[TestMethod]
public void GetPersonsTest()
{
var address = new Address { Id = 1, Line1 = "123 Main St." };
var expectedPersons = new List<Person>
{
new Person { Id = 1, Address = address, Name = "John" },
new Person { Id = 2, Address = address, Name = "John Jr." },
};
var mockPersonSet = GetMockDbSet(expectedPersons.AsQueryable());
mockPersonSet.Setup(m => m.Include("Address")).Returns(mockPersonSet.Object);
var mockPersonContext = new Mock<PersonContext>();
mockPersonContext.Setup(o => o.Persons).Returns(mockPersonSet.Object);
// test the controller GetPersons() method, which leverages Include()
var controller = new PersonsController(mockPersonContext.Object);
var actualPersons = controller.GetPersons();
CollectionAssert.AreEqual(expectedPersons, actualPersons.ToList());
}
// a helper to make dbset queryable
private Mock<DbSet<T>> GetMockDbSet<T>(IQueryable<T> entities) where T : class
{
var mockSet = new Mock<DbSet<T>>();
mockSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(entities.Provider);
mockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(entities.Expression);
mockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(entities.ElementType);
mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(entities.GetEnumerator());
return mockSet;
}
For anyone who stumbles upon this issue with interest on how to solve the .Include("Foo")
problem with NSubstitute and Entity Framework 6+, I was able to bypass my Include
calls in the following way:
var data = new List<Foo>()
{
/* Stub data */
}.AsQueryable();
var mockSet = Substitute.For<DbSet<Foo>, IQueryable<Foo>>();
((IQueryable<Post>)mockSet).Provider.Returns(data.Provider);
((IQueryable<Post>)mockSet).Expression.Returns(data.Expression);
((IQueryable<Post>)mockSet).ElementType.Returns(data.ElementType);
((IQueryable<Post>)mockSet).GetEnumerator().Returns(data.GetEnumerator());
// The following line bypasses the Include call.
mockSet.Include(Arg.Any<string>()).Returns(mockSet);