I have been doing some development where I need to mock out a TFS server and I have come against an issue with mocking some of the collections. I have worked round it, but am interested to know what is happening, if anyone knows.
In general for mocking TFS collections I have created a List<T> and swapped it in as follows
var fakeTypes = new List<WorkItemType>();
for (int count = 0; count < 2; count++)
{
var fakeType = Isolate.Fake.Instance<WorkItemType>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => fakeType.Name).WillReturn(string.Format("Type {0}", count));
fakeTypes.Add(fakeType);
}
// using a previous created fakeStore
Isolate.WhenCalled(() => fakeStore.Projects["Project 0"].WorkItemTypes).WillReturnCollectionValuesOf(fakeTypes);
In most cases this form works, I can iterate through the collection.
foreach (WorkItemType item in project.WorkItemTypes)
{
// do something
var name = item.Name;
}
However in the particular case of the WorkItemTypes this did not work, it compiles, but the loop was never entered i.e. the collection was empty.
I found that the following form of fake object creation works
var fakeCollection = Isolate.Fake.Instance<WorkItemTypeCollection>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => fakeStore.Projects["Project 0"].WorkItemTypes).WillReturn(fakeCollection);
// now we set the responses when each item is called
// using the list<T> create at the start
Isolate.WhenCalled(() => fakeCollection[0]).WillReturn(fakeTypes[0]);
Isolate.WhenCalled(() => fakeCollection[1]).WillReturn(fakeTypes[1]);
This form does work for WorkItemTypeCollection, so my question is what is the recommended way mock collections, should both of these forms work or is there a good reason why the first form fails in some cases