Modifying the Method Behavior
You can specify how you want a method to behave when you call it. For example, you can define that when the method is called, its original implementation should be invoked. Alternatively, you can force throwing an exception or returning a test collection of data.
When to Use
You can modify the behavior of a specific fake method when your test requires a specific return value from the method.
To ensure that minor changes to production code will not break your test, by default, Typemock Isolator ignores all arguments passed to fake methods. See here how to fake methods based on call arguments.Syntax
C# Isolate.WhenCalled(() => fake.Increment()).<behavior>;
VB
Isolate.WhenCalled(Function() fake.Increment()).<behavior>
The following table explains possible behaviors:
Behavior |
Description |
When the method is called, call the original implementation. |
|
Returns: •For reference types: fake objects •For other return types: zero or equivalent
|
|
Throws a specified exception. |
|
Return a specified value. This is applicable only to those methods that return values. |
|
Returns immediately. This is applicable only to void methods. |
|
Returns a collection of test data. |
|
Calls a user code. This option is used for advanced and complex behaviors. |
Samples
Sample 1: Ignoring Call
C# Isolate.WhenCalled(() => fake.Increment()).IgnoreCall();
VB
Isolate.WhenCalled(Sub() fake.Increment()).WillBeIgnored()
Sample 2: Setting Different Behavior on Overloaded Methods
To set a different behavior to overloaded methods, use WhenCalled() on each overload.
The last overload method behavior setting is the default behavior for all overloads.
C# // Note: Use Isolated to clean up after the test [TestMethod, Isolated] public void Overloaded_Example() { var fakeDependency = Isolate.Fake.Instance<Dependency>(); // Each overloaded method will act as a separate sequence Isolate.WhenCalled(() => fakeDependency.OverloadedMethod(1)).WillReturn(2); Isolate.WhenCalled(() => fakeDependency.OverloadedMethod("Typemock Rocks")).WillReturn(9); var result = new ClassUnderTest().AddOverloadedDependency(fakeDependency); Assert.AreEqual(11, result); } public int AddOverloadedDependency(Dependency dependency) { return dependency.OverloadedMethod(12) + dependency.OverloadedMethod("typemock"); }
VB
' Note: Use Isolated to clean up after the test
<TestMethod(), Isolated()> _
Public Sub Overloaded_Example()
Dim fakeDependency = Isolate.Fake.Instance(Of Dependency)()
' Each overloaded method will act as a separate sequence
Isolate.WhenCalled(Function() fakeDependency.OverloadedMethod(1)).WillReturn(2)
Isolate.WhenCalled(Function() fakeDependency.OverloadedMethod("Typemock Rocks")).WillReturn(9)
Dim result = New ClassUnderTest().AddOverloadedDependency(fakeDependency)
Assert.AreEqual(11, result)
End Sub
Public Function AddOverloadedDependency(dependency As Dependency) As Integer
Return dependency.OverloadedMethod(12) + dependency.OverloadedMethod("typemock")
End Function
Isolator also have an option to change the method logic using the DoInstead feature.