This section takes you through some of the basics of Typemock Isolator
- Faking Objects
- Behaviors
- Verifying Behaviors
Don’t forget that to use Typemock Isolator, you need to reference TypeMock Isolator C# APIsand TypeMoke Isolator Core DLL (via the .NET tab), or add the actual files named TypeMock.dll and TypeMock.Arrange.Act.Assert.Dll (via the Browse tab).
Faking Objects
Creating Fake Objects
Fake objects replace real objects in the test. We create fake objects to isolate objects that represent an external dependency in your test. Their behavior is set up using Isolate.WhenCalled() and verified using Isolate.Verify.
To create a fake object, use
Isolate.Fake.Instance() method: RealLogger fake = Isolate.Fake.Instance();
Members.MustSpecifyReturnValues: Before using methods that return a value on the fake object, their behavior must be defined.
- Members.CallOriginal: The fake object will pass through any calls made on it to their original implementation.
- Members.ReturnNulls: The fake object will return zero or equivalent to any call on methods returning value types, and null for any methods returning a reference type. Void calls are ignored.
Members.ReturnRecursiveFake is the default fake object behavior. This means that calling Isolate.Fake.Instance() is the same as passing in Members.ReturnRecursiveFake.
Members.ReturnRecursiveFake behavior is a powerful faking tool, automatically faking chained calls even several levels down the object model. No need to explicitly fake return values and behavior for each hierarchy level.
In this example, the fake RealLogger we create calls GetSon() which in turn returns another a Son object on which another call is made:11111111111
[TestMethod]
[Isolated]
public void RecursiveStubWithExpectationOnSecondLevel ()
{
/ Arrange
RealLogger fake = Isolate.Fake.Instance(Members.ReturnRecursiveFake);
Isolate.WhenCalled(() => fake.GetSon().DoSomething()).WillReturn(10);
/ Act
Logger son = fake.GetSon();
son.DoSomething();
/ Assert
Isolate.Verify.WasCalledWithAnyArguments(() => fake.GetSon().DoSomething());
}
Future Instances
Sometimes we want fake the runtime behavior for objects instantiated in the code under test, also known as ‘Future instance’. We set up the objects as shown above, to be swapped with the future object at the time of its creation. This is done using the Isolate.Swap.NextInstance().With() method.
1 2 3 4 5 6 7 8 9 10 |
public void FutureInstance_VerifyMethodWasCalledAndStubbed () { / Arrange RealLogger fake = Isolate.Fake.Instance(); / replace a future instance of RealLogger with the fake object we created Isolate.Swap.NextInstance().With(fake); / set up additional fake object behavior Isolate.WhenCalled(() => fake.Increment()).IgnoreCall(); } |
Controlling Behavior
Method Behavior
When you create fake objects, all method calls are handled according to its default behavior, specified on creation. You can set specific method’s behavior by using Isolate.WhenCalled():
1 2 |
RealLogger fake = Isolate.Fake.Instance(); Isolate.WhenCalled(() => fake.Increment()).CallOriginal(); |
Isolate.WhenCalled() sets up completing statements that specify the desired method behavior, such as:
- CallOriginal() – Uses the original implementation
- WillThrow() – Throws the specified exception
- WillReturn() – Returns specified value This is applicable only to methods returning values
- IgnoreCall() – Ignores the call This is applicable only to void methods
- ReturnRecursiveFakes() – Returns zero or equivalent to any call on methods returning value types, and return fake objects for any methods returning a reference type. The returned fake objects will behave in the same way.
- ReturnRecursiveFakes() – Returns a fake of a specific type. The returned object will return a zero or equivalent to any call on methods returning value types, and return fake objects for any methods returning a reference type.
- WillReturnCollectionValuesOf() – Swaps a collection returned by a method or property with a collection of test data.
- DoInstead() – User-specified behavior
Isolate.WhenCalled() is strongly typed, which enforces correct use, for example:
12 Isolate.WhenCalled(() => fake.ReturnInt()).WillReturn(10); / this is fineIsolate.WhenCalled(() => fake.ReturnInt()).WillReturn("ten");/ this will not compile
Static Method Behavior
Static methods are called on class types, not on fake instances, so there is no creation involved when setting up behavior for fake static method. Instead, we use Isolate.Fake.StaticMethods():
1 |
Isolate.Fake.StaticMethods(); |
Static classes cannot be passed as type arguments to StaticMethods(), so an overload is provided to pass them as type parameters:
1 Isolate.Fake.StaticMethods(typeof(StaticClassToFake));
For example, here’s how you test static methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[TestMethod] [Isolated] public void TestStaticMethods() { / define static method faking behavior for LoggerFactory Isolate.Fake.StaticMethods(Members.CallOriginal); / define specific faking behavior for a chained call starting with static call Isolate.WhenCalled(() => LoggerFactory.GetLogger().GetSon().DoSomething()).WillReturn(10); Assert.AreEqual(10, LoggerFactory.GetLogger().GetSon().DoSomething()); / Verify a static call was made Isolate.Verify.WasCalledWithAnyArgumnets(() => LoggerFactory.GetLogger().GetSon()); } |
Faking Behavior for Live Objects
A live object is a test object which has been instantiated normally, not through Isolator. When you only want to modify an object’s behavior for a specific method call, just fake the specific behavior using Isolate.Fake.Instance():
1 2 |
RealLogger logger = new RealLogger(); / this is a normal "live" object Isolate.WhenCalled(() => logger.Log("message")).IgnoreCall(); / ignore only the Log method |
Faking Calls According to Method’s Arguments
Custom behavior can be set based on arguments. This example shows you how to use exact argument matching using WhenCalled(() => …).WithExactArguments(). The behavior specified after WithExactArguments() will only happen when the arguments in the call exactly match to the arguments passed to WhenCalled().
For example:
1 2 3 4 5 6 7 8 9 10 11 |
[TestMethod, Isolated] public void FakeReturnValueByMethodArgs() { var fake = Isolate.Fake.Instance(); / MethodReturnInt will return 10 when called with arguments 3, "abc" Isolate.WhenCalled(()=> fake.MethodReturnInt(3, "abc")).WithExactArguments().WillReturn(10); / MethodReturnInt will return 50 when called with arguments 3, "xyz" Isolate.WhenCalled(()=> fake.MethodReturnInt(3, "xyz")).WithExactArguments().WillReturn(50); Assert.AreEqual(10, fake.MethodReturnInt(3, "abc")); Assert.AreEqual(50, fake.MethodReturnInt(3, "xyz")); } |
As you can see, we set the return value of MethodReturnInt to 10 only when the arguments are 3 and “abc”, and set the return value of MethodReturnInt to 50 only when the arguments are 3 and “xyz”.
Verifying Behaviors
When writing unit tests, you may want to make sure that specific calls were made or correct parameters were passed into function calls. This is easily done with Isolate.Verify with a completing verification statement.
- WasCalledWithAnyArguments() – Verifies the call was made at least once during the test execution
- WasCalledWithExactArguments() – Verifies the call was made at least once during the test execution with the provided arguments
- WasNotCalled() – Verifies the call wan’t called during the test execution
- WasCalledWithArguments() – Verifies the call was made at least once with arguments matching a predicate
Verification is performed on fake objects and goes over the information collected during the test execution for them.