Faking Behavior for Live Objects
A live object is a test object that was instantiated normally and is not a fake. Typemock Isolator supports controlling methods of live objects.
When to Use
When you want to fake a method of an existing object.
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(() => dependency.<method>).<behavior>;
VB
Isolate.WhenCalled(Function() dependency.<method>).<behavior>
The following table explains possible behavior:
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. | |
Return a collection of test data. | |
Calls a user code. This option is used for advanced and complex behaviors. |
Samples
Sample 1: Ignoring a Method
The following sample shows how to ignore a method of a live object.
Creating live objects is similar to creating a fake object using Members.CallOriginal().
To fake non-public methods, use Isolate.NonPublic.WhenCalled().
C# var dependency = new Dependency(); // not a fake Isolate.WhenCalled(() => dependency.CheckSecurity(null,null)).IgnoreCall(); // Ignore CheckSecurity method
VB
Dim dpendency = New Dependency()
Isolate.WhenCalled(Sub() dpendency.CheckSecurity(Nothing, Nothing)).IgnoreCall()
Sample 2: Faking the Internal Property Behavior
The following sample shows how to fake the behavior of a live object’s property.
C# Dependency dependency = new Dependency(); // faking internal property behavior on real object Isolate.NonPublic.Property.WhenGetCalled(dependency, "IsDiskFull").WillReturn(true);
VB
Dim dependency = New Dependency()
Isolate.NonPublic.Property.WhenGetCalled(dependency, "IsDiskFull").WillReturn(True)