Behavior Sequencing is one of the cool features that we’ve added to Isolator AAA API.
It enables the developer to set a sequence of expectations on the same method/property to simulate a need behavior on a faked object.
Warning – if you think you’re going to find here a new method on top of our API you’re dead wrong. Behavior Sequencing is a feature that just works without the user needing to find the right "magic" word in the documentation, all that is need is to set more then on expectation on the same method/property:
[TestMethod]
public void WIllReturnNull_ThenReturnRecursiveFake()
{
Isolate.Fake.StaticMethods<ProductFactory>();
//The 1st time called the product shelf shall return null
Isolate.WhenCalled(() => ProductFactory.GetProduct("MP3 Player")).WillReturn(null);
// Afterwards return a fake product
Isolate.WhenCalled(() => ProductFactory.GetProduct("MP3 Player")).ReturnRecursiveFake();
// The 1st call will return null
var product = ProductFactory.GetProduct("MP3 Player");
Assert.IsNull(product);
// All of the calls after the 1st will return a faked product
Assert.IsNotNull(ProductFactory.GetProduct("MP3 Player"));
Assert.IsNotNull(ProductFactory.GetProduct("MP3 Player"));
}
The rules of this feature are easy – each time the call is made the next behavior is returned. The last call in the sequence is the default behavior that will be repeated after all sequenced behaviors are used.
And in case you’re using our brand new Visual Basic Friendly API:
<TestMethod()> _
Public Sub TwoConsecutiveCallsToWillReturn()
Dim fakeProduct As Product = FakeInstance(Of Product)()
‘ Calling ThsesCalls once cause the value to be the default return value
‘ Because we are using peoperty getter we need a dummy variable
Using TheseCalls.WillReturn(20.0F)
Dim dummy As Single = fakeProduct.Price
End Using
‘ this call cause the previous behavior to happen once and this behavior becomes the default returned value
‘ Because we are using peoperty getter we need a dummy variable
Using TheseCalls.WillReturn(15.0F)
Dim dummy As Single = fakeProduct.Price
End Using
‘ Calling Price the 1st time return 20
Assert.AreEqual(20.0F, fakeProduct.Price)
‘ fakeProduct.Price will return 15 for the rest of the calls
Assert.AreEqual(15.0F, fakeProduct.Price)
Assert.AreEqual(15.0F, fakeProduct.Price)
End Sub
To futher increase the usability of this feature we’ve added two more modifiers to the WhenCalled/TheseCalls:
- ReturnRecursiveFakes – calling this method will return a fake object (same as creating a new instance with Members.RecursiveFakes).
- WillCallOriginal/CallOriginal – call the real implementation of the function.
Now developers can create fake objects that has sequenced behavior.