The Basics mocking and faking with Typemock Isolator

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 Typemock 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

the Isolate.Fake.Instance() method: RealLogger fake = Isolate.Fake.Instance();

Fake objects can be created with a few predefined behaviors, that set the fake object’s default behavior. You define the behavior types by passing a Members enum parameter toIsolate.Fake.Instance(). The possible behavior types are:

  • Members.ReturnRecursiveFake: The fake object will return 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.
  • 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:

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.

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():

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:

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():

Static classes cannot be passed as type arguments to StaticMethods(), so an overload is provided to pass them as type parameters:

For example, here’s how you test static methods:

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():

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:

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.