I'm running the evaluation of TypeMock Isolator to see if I can run tests on a simple graph of Entity Framework 3.5 objects without having to access the database, but I'm having problems assembling a parent-child graph correctly.
I have a Parent EF object which contains a navigation to an EntityCollection of Child objects. I'm trying to pass the faked Parent (with faked Children) to a Business Object which reads the child objects, but I can't seem to get the child objects to appear in the mocked parent.
In this example, I'm trying to test my business method, "DoSomeStuff", which in turn calls "RetrieveParentsToDoSomeStuff". It also calls the auto-generated Entity Framework "Parent.Children" to retrieve an EntityCollection of children. The problem is that the parent.Children collection doesn't contain my dummy child collection, even though my assertion passes that "Children.Load" is called. (I'm not sure how to assert that "parent.Children" returned a non-zero-length collection, but I can see that this is the case in the debugger.)
Is this the correct way to do this? I also tried using real EntityObjects, but the ObjectContext seemed to think that they hadn't been initialized properly (it complained that they seemed to be from another ObjectContext).
I've seen
http://mosesofegypt.net/post/Introducin ... lator.aspx, but there the ObjectContext is modified; I'm trying to avoid that in my tests....
// Create a Child entity object
Child dummyChild = Isolate.Fake.Instance<Child>();
dummyChild.Value1 = 1;
dummyChild.Value2 = 2;
// put it in a collection
IQueryable<Child> fakedChildren = (new List<Child> { child }).AsQueryable();
// Create a Parent entity object
Parent dummyParent = Isolate.Fake.Instance<Parent>();
dummyParent.Name = "Dummy Parent";
// ReSharper disable ConverClosureToMethodGroup
Isolate.WhenCalled(() => dummyParent.Children.IsLoaded).WillReturn(false);
Isolate.WhenCalled(() => dummyParent.Children.Load()).IgnoreCall();
Isolate.WhenCalled(() => dummyParent.Children).WillReturnCollectionValuesOf(fakedChildren);
// Make a collection of the one Parent
List<Parent> fakedParents = new List<Parent>
{
dummyParent
};
// Intercept the internal call and replace the return value with our fakeParents.
Isolate.NonPublic.WhenCalled(businessObject, "RetrieveParentsToDoSomeStuff").WillReturn( fakedParents);
// *** ACT ***
// DoSomeStuff will call "RetrieveParentsToDoSomeStuff"
bool stuffDone = businessObject.DoSomeStuff();
// *** ASSERT ***
Isolate.Verify.WasCalledWithAnyArguments(() => dummyParent.Children.Load());
// *** I can't figure out how to assert that dummyParent.Children actually
// returned an non-zero-length EntityCollection, but I can see that it
// contains no elements in the debugger.