My test fixture includes this utility method to create a populated SPList:
private SPList MakeList()
{
SPList fakeList = Isolate.Fake.Instance<SPList>(Members.CallOriginal);
for (int i = 1; i <= 6; i++)
{
Isolate.WhenCalled(() => fakeList.Items[i]).WillReturn(MakeTaskItem(i, "This is task number " + i.ToString()));
}
Isolate.WhenCalled(() => fakeList.GetItemById(1)).WillReturn(MakeTaskItem(1, "This is task number 1"));
Isolate.WhenCalled(() => fakeList.GetItemById(2)).WillReturn(MakeTaskItem(2, "This is task number 2"));
Isolate.WhenCalled(() => fakeList.GetItemById(3)).WillReturn(MakeTaskItem(3, "This is task number 3"));
Isolate.WhenCalled(() => fakeList.GetItemById(4)).WillReturn(MakeTaskItem(4, "This is task number 4"));
Isolate.WhenCalled(() => fakeList.GetItemById(5)).WillReturn(MakeTaskItem(5, "This is task number 5"));
Isolate.WhenCalled(() => fakeList.GetItemById(6)).WillReturn(MakeTaskItem(6, "This is task number 6"));
return fakeList;
}
I have two tests:
[Test]
public void FakeList_Items_3()
{
SPWeb spWeb = Isolate.Fake.Instance<SPWeb>(Members.ReturnRecursiveFakes);
SPList spList = MakeList();
SPListItem thisItem = spList.Items[3];
Assert.That(thisItem.Title, Is.EqualTo("This is task number 3"));
}
[Test]
public void FakeList_GetItemByID_3()
{
SPWeb spWeb = Isolate.Fake.Instance<SPWeb>(Members.ReturnRecursiveFakes);
SPList spList = MakeList();
SPListItem thisItem = spList.GetItemById(3);
Console.WriteLine(thisItem.Title);
Assert.That(thisItem.Title, Is.EqualTo("This is task number 3"));
}
The FakeList_Items_3 succeeds, but the FakeList_GetItemByID_3 fails. The thisItem object is always the first one, not the third. IOW, thisItem.Title is "This is task number 1", even though I asked for number 3.
How can I mock the GetItemById method? I use it all over the place in my actual code.