Sample 1: Faking items retrieval from a SharePoint site

Original Method

The following sample includes a function that retrieves all of the items from a list called Tasks. The list has the Urgent priority.

C#

public List<string> GetUrgentTasks()
{
  var site = new SPSite("http://sharepoint.typemock.com");

  var taskList = site.OpenWeb().Lists[TASKS_LIST_NAME];
  var urgentTasks = new List<string>();

  foreach (SPListItem item in taskList.Items)
  {
    if (item[PriorityFieldName] != null &&
        item[PriorityFieldName].ToString() == Priority.Urgent.ToString())
    {
      urgentTasks.Add(item.Name);
    }
  }
 
  return urgentTasks;
}

VB

Public Shared Function GetUrgentTasks() As List(Of String)
    Dim site = New SPSite("http://sharepoint.typemock.com")

    Dim taskList = site.OpenWeb().Lists(TASKS_LIST_NAME)
    Dim urgentTasks = New List(Of String)()

    For Each item As SPListItem In taskList.Items
        If item(PriorityFieldName) IsNot Nothing AndAlso item(PriorityFieldName).ToString() = Priority.ePriority.Urgent.ToString() Then
            urgentTasks.Add(item.Name)
        End If
    Next

    Return urgentTasks
End Function

Typemock Isolator Features Used

Recursive Fakes

Swap Future Instance

Implicit Collection Creation

Scenario

1. Fake the SPSite class. Because you need all of the object-model classes instantiated from SPSite to be faked as well, use the Recursive Fakes feature.

2. Because SPSite is instantiated inside the test code, use Swap.NextInstance to define that the next instance of SPSite will be replaced with the faked one.

3. Populate the fake SPList object with items. Using the implicit collection creation, you can define a collection of 10 items and populate them with only two items.

4. Call GetUrgentTasks() and check the result using Assertions.

Code
C#

[TestMethod, Isolated]
public void GetUrgentTasks_QuerySiteForAllTasksAndReturnTheNamesOfTheUrgentTasks_TwoUrgentTasksFound()
{
  // Fake recursively the next SPSite and all its returned classes are faked
  var fakeSite = Isolate.Fake.NextInstance<SPSite>();

  // Retrieve a fake SPList to set behavior
  var fakeTaskList = fakeSite.OpenWeb().Lists[SharePointLogic.TASKS_LIST_NAME];
  
  // Isolator will automatically create an internal fake list with 10 items in it (based on the highest indexer)
  // Two of those items (1 and 9) have "Urgent" Priority ,while the rest are not important for the test
  string urgentPriorityString = SharePointLogic.Priority.Urgent.ToString();
  var priorityFieldName = SharePointLogic.PriorityFieldName;

  Isolate.WhenCalled(() => fakeTaskList.Items[1][priorityFieldName]).WillReturn(urgentPriorityString);
  Isolate.WhenCalled(() => fakeTaskList.Items[9][priorityFieldName]).WillReturn(urgentPriorityString);
  Isolate.WhenCalled(() => fakeTaskList.Items[1].Name).WillReturn("Do the laundry");
  Isolate.WhenCalled(() => fakeTaskList.Items[9].Name).WillReturn("Wash the dishes");

  // Call The Method under test
  var urgentTasks = classUnderTest.GetUrgentTasks();
  Assert.AreEqual(2, urgentTasks.Count);
  Assert.AreEqual("Do the laundry", urgentTasks[0]);
  Assert.AreEqual("Wash the dishes", urgentTasks[1]);
}

VB

<TestMethod(), Isolated()> 
Public Sub GetUrgentTasks_QuerySiteForAllTasksAndReturnTheNamesOfTheUrgentTasks_TwoUrgentTasksFound()
    ' Fake recursively the next SPSite and all its returned classes are faked
    Dim fakeSite = Isolate.Fake.NextInstance(Of SPSite)()

    ' Retrieve a fake SPList to set behavior
    Dim fakeTaskList = fakeSite.OpenWeb().Lists(SharePointLogic.TASKS_LIST_NAME)

    ' Isolator will automatically create an internal fake list with 10 items in it (based on the highest indexer)
    ' Two of those items (1 and 9) have "Urgent" Priority ,while the rest are not important for the test
    Dim urgentPriorityString As String = SharePointLogic.Priority.ePriority.Urgent.ToString()
    Dim priorityFieldName = SharePointLogic.PriorityFieldName

    Isolate.WhenCalled(Function() fakeTaskList.Items(1)(priorityFieldName)).WillReturn(urgentPriorityString)
    Isolate.WhenCalled(Function() fakeTaskList.Items(9)(priorityFieldName)).WillReturn(urgentPriorityString)
    Isolate.WhenCalled(Function() fakeTaskList.Items(1).Name).WillReturn("Do the laundry")
    Isolate.WhenCalled(Function() fakeTaskList.Items(9).Name).WillReturn("Wash the dishes")

    ' Call The Method under test
    Dim urgentTasks = SharePointLogic.GetUrgentTasks()
    Assert.AreEqual(2, urgentTasks.Count)
    Assert.AreEqual("Do the laundry", urgentTasks(0))
    Assert.AreEqual("Wash the dishes", urgentTasks(1))
End Sub