Controlling Collections and Indexers
To return fake collections which are enumerable, use Isolate.WhenCalled.WillReturnCollectionValuesOf().
When Isolate.WhenCalled.WillReturnCollectionValuesOf() is used, Typemock Isolator will implicitly replace calls of GetEnumerator to the original collection with calls to GetEnumerator of the test collection.
When to Use
You can control collections and indexers when your test requires that a fake method to return an enumerable collection.
Syntax
C# Isolate.WhenCalled(() => <your_collection>).WillReturnCollectionValuesOf(<items>);
VB
Isolate.WhenCalled(Function() <your_collection>).WillReturnCollectionValuesOf(<items>)
Samples
Typemock Isolator does not replace collections that are defined in mscorlib.dll.
C# [TestMethod, Isolated] public void SwapCollection_WithFakeData() { var dependency = new Dependency(); Isolate.WhenCalled(() => dependency.GetList()).WillReturnCollectionValuesOf(new int[] { 1, 2, 3 }); var result = new ClassUnderTest().Sum(dependency); Assert.AreEqual(6, result); } public int Sum(Dependency dependency) { int total = 0; foreach (var i in dependency.GetList()) { total += i; } return total; }
VB
<TestMethod, Isolated>
Public Sub SwapCollection_WithFakeData()
Dim dependency = New Dependency()
Isolate.WhenCalled(Function() dependency.GetList()).WillReturnCollectionValuesOf(New Integer() {1, 2, 3})
Dim result = New ClassUnderTest().Sum(dependency)
Assert.AreEqual(6, result)
End Sub
Public Function Sum(dependency As Dependency) As Integer
Dim total As Integer = 0
For Each i As Integer In dependency.GetList()
total += i
Next
Return total
End Function
The WillReturnCollectionValuesOf() method is not sequential.
Meaning that if you set two diffrent behaviors that returns two diffrent collections to the same method it will only use the first one.