Using the EntityFramework.Testing.Moq repository provided by Rowan Miller. I'm trying to mock an entire EF6 Context, including mock instances of all the DBSet's. Data for each DBSet is loaded from an external source into a list of Model Entities.
The Goal is to mock a Context object that can be reused across multiple(and ideally all) Unit Tests in a solution. Below is a simplified example of my initial Context setup:
Mock<MyContext> context = new Mock<MyContext>();
context.Setup(c => c.Entity1).Returns(LoadEntities<Entity1>().Object);
context.Setup(c => c.Entity2).Returns(LoadEntities<Entity2>().Object);
context.Setup(c => c.Entity3).Returns(LoadEntities<Entity3>().Object);
This will setup the DBSet's for each Model Entity Type. The "LoadEntities()" is a generic method that returns a MockDbSet.
The mocked data works fine when querying against a single DBSet, but if one entity contains a collection of another entity, the collection is always empty!
Is there a way to "associate" two DBSet's when setting up the mocked Context?
(Basic Info: I'm using the Model First approach)