Faking Indexers
When you fake indexers, only the specified index values will behave according to the specified behavior. Other indices will behave according to the fake object default behavior.
When to Use
You can fake indexers when your test requires to fake an indexer method.
Syntax
C# Isolate.WhenCalled(()=> <fake>[<key>]).<behavior>;
VB
Isolate.WhenCalled(Function() <fake>(<key>)).<behavior>
Samples
The following sample shows how to fake the behavior index 2 of the indexer. All other indexes call the original implementation.
C# [TestMethod] public void TrueIndexersTest() { var fake = Isolate.Fake.Instance<ClassWithIndexer>(Members.CallOriginal); // We fake only the return value of index 2 Isolate.WhenCalled(()=> fake[2]).WillReturn("Dog"); // Here we call non fake indexer Assert.AreEqual("Cat", fake[0]); // Calling our fake indexer Assert.AreEqual("Dog", fake[2]); } public class ClassWithIndexer { public string this[int index] { get { return "Cat"; } } }
VB
<TestMethod(), Isolated()>
Public Sub TrueIndexersTest()
Dim fake = Isolate.Fake.Instance(Of ClassWithIndexer)(Members.CallOriginal)
' We fake only the return value of index 2
Isolate.WhenCalled(Function() fake(2)).WillReturn("Dog")
' Here we call non fake indexer
Assert.AreEqual("Cat", fake(0))
' Calling our fake indexer
Assert.AreEqual("Dog", fake(2))
End Sub
Public Class ClassWithIndexer
Default Public ReadOnly Property Item(index As Integer) As String
Get
Return "Cat"
End Get
End Property
End Class
Default Values of Undefined Indexes
Creating fake collections just to access a specific index can be a tedious task. Typemock Isolator automatically creates a complete collection as soon as Isolate.WhenCalled() is called on a specific integer index.
To implicitly create a fake collection, call the last index of the collection that you want to fake in Isolate.WhenCalled(). For example, Isolate.WhenCalled(() => myCollection[19]) will create 20 item collections.
Samples
This feature works only on collections that are not defined in the mscorlib.dll assembly. In addition, this feature works only when using indexers with an integer argument. Typemock Isolator cannot identify the collection size if the indexer argument is not a numeric type.
C# [TestMethod, Isolated] public void ImplictCollectionCreation_ByFakingLastItem() { var dependency = new Dependency(); // A fake collection of size of 6 is created Isolate.WhenCalled(() => dependency.GetList()[5]).WillReturn(3); var result = new ClassWithIndexer().Count(dependency); Assert.AreEqual(6, result); Assert.AreEqual(3, dependency.GetList()[5]); } public class ClassWithIndexer { public int Count(Dependency dependency) { return dependency.GetList().Count; } }
VB
<TestMethod(), Isolated()>
Public Sub ImplictCollectionCreation_ByFakingLastItem()
Dim dependency = New Dependency()
' A fake collection of size of 6 is created
Isolate.WhenCalled(Function() dependency.GetList()(5)).WillReturn(3)
Dim result = New ClassWithIndexer().Count(dependency)
Assert.AreEqual(6, result)
Assert.AreEqual(3, dependency.GetList()(5))
End Sub
Public Class ClassWithIndexer
Public Function Count(dependency As Dependency) As Integer
Return dependency.GetList().Count
End Function
End Class