Simulating a Sequenced Behavior
A sequenced behavior occurs when the same method has a different behavior each time the method is called.
When to Use
When you want to simulate a sequenced behavior.
After using a
sequence and calling the method with the expected behavior, you can use
WhenCalled on the method again. In this case, the old behavior sequence is
reset, and a new one starts from the next WhenCalled.
Syntax
C# Isolate.WhenCalled(() => fakeDependency.GetID()).<behavior>; Isolate.WhenCalled(() => fakeDependency.GetID()).<behavior>; ...
VB
Isolate.WhenCalled(Function() fakeDependency.GetID()).<behavior>
Isolate.WhenCalled(Function() fakeDependency.GetID()).<behavior>
...
The last call to Isolate.WhenCalled() defines the default behavior for the method from that moment onwards.
Samples
C# // Note: Use Isolated to clean up after the test [TestMethod, Isolated] public void SequencedWillReturn_Example() { var fakeDependency = Isolate.Fake.Instance<Dependency>(); // Sequenced calls will return values in sequence, last value will stay the default Isolate.WhenCalled(() => fakeDependency.GetID()).WillReturn(2); Isolate.WhenCalled(() => fakeDependency.GetID()).WillReturn(9); var result = new ClassUnderTest().AddToDependency3Times(1, fakeDependency); Assert.AreEqual(21, result); } public int AddToDependency3Times(int a, Dependency dependency) { return a + dependency.GetID() + dependency.GetID() + dependency.GetID(); }
VB
' Note: Use Isolated to clean up after the test
<TestMethod(), Isolated()> _
Public Sub SequencedWillReturn_Example()
Dim fakeDependency = Isolate.Fake.Instance(Of Dependency)()
' Sequenced calls will return values in sequence, last value will stay the default
Isolate.WhenCalled(Function() fakeDependency.GetID()).WillReturn(2)
Isolate.WhenCalled(Function() fakeDependency.GetID()).WillReturn(9)
Dim result = New ClassUnderTest().AddToDependency3Times(1, fakeDependency)
Assert.AreEqual(21, result)
End Sub
Public Function AddToDependency3Times(a As Integer, dependency As Dependency) As Integer
Return a + dependency.GetID() + dependency.GetID() + dependency.GetID()
End Function
