Setting the Default Behavior of a Fake Object

You can set a default behavior of all methods of a class or type. Then you can specify a different behavior for a specific method.

The following default behaviors are available:

Behavior

Behavior

Members.ReturnRecursiveFakes

Returns:

For reference types: fake objects

For other return types: zero or equivalent


The returned fake objects will behave in the same way.

This is the default fake object behavior. Using this option will ensure that your unit tests are stable.

Members.CallOriginal

Calls to fake object methods will pass through to their original implementation. The object's real constructor is called during the creation of the fake object. This is useful when you need to maintain the original behavior of the test object and modify the behavior of specific methods.

Members.MustSpecifyReturnValues

Calls to fake object methods that return a specific value. If a method that returns the specified value is called without being set up in first place, an exception is thrown, and the test will fail. Void calls are still ignored.

Members.ReturnNulls

Calls to fake object methods will return:

zero or equivalent: to any call of methods that return value types

null: for any methods that return a reference type.


Void calls are still ignored.

Members.MustBeSpecified

Every method of the mocked object must be defined or Typemock Isolator will throw an exception.

Syntax

C#

 var fake = Isolate.Fake.Instance<ClassUnderTest>(Members.<behavior>);

VB

 Dim fake = Isolate.Fake.Instance(Of ClassUnderTest)(Members.<behavior>);

Samples

Sample 1: Using Recursive Fakes

An ability to fake objects is a powerful and versatile tool. Even if the code under test calls an intricate object model which is replaced by a fake object, deep calls (chained calls several levels down the object model) will be faked automatically. Therefore, you do not have to explicitly fake return values and define behavior for each and every hierarchy level.

The following example shows how to use Members.ReturnRecursiveFakes () (default behavior).

For collections in mscorlib (which implements IEnumerable and have default constructor) Members.ReturnRecursiveFake() returns an initialized empty collection.

C#

[TestMethod, Isolated]
public void FakeObjectExample()
{
  // Arrange
  var fake = Isolate.Fake.Instance<Process>(Members.ReturnRecursiveFakes);
  Isolate.WhenCalled(() => fake.MainModule.Site.Name).WillReturn("Typemock rocks");

  // Act
  var result = MyCode.IsMySiteNameTypemock(fake);

  // Assert
  Assert.AreEqual(true, result);
}

public class MyCode
{
  public static bool IsMySiteNameTypemock(Process process)
  {
    var name = process.MachineName;

    if (process.MainModule.Site.Name.StartsWith("Typemock"))
      return true;
    else
      return false;
  }
}

VB

<TestMethod(), Isolated()> 
Public Sub BasicTestStructure()
  ' Arrange
  Dim fake = Isolate.Fake.Instance(Of Process)(Members.ReturnRecursiveFakes) 
  Isolate.WhenCalled(Function() fake.MainModule.Site.Name).WillReturn("Typemock rocks")

  ' Act
  Dim result = My_Class.IsMySiteNameTypemock(fake)

  ' Assert
  Assert.AreEqual(True, result)
End Sub

Public Class MyCode
  Public Shared Function IsMySiteNameTypemock(process As Process) As Boolean
    Dim name = process.MachineName

    If process.MainModule.Site.Name.StartsWith("Typemock") Then
      Return True
    Else
      Return False
    End If
  End Function
End Class

Sample 2: Using Stale Mocks

A stale mock is an object that was faked in a previous test and currently used in another test, this may lead to an unexpected behavior, such as an error message that will be printed to the console.
If you do not want this message to be printed to the console you will need to use the [Isolated] attribute with the IgnoreStaleMocks property.

C#
[Isolated(IgnoreStaleMocks = true)]

VB
<Isolated(IgnoreStaleMocks:=True)>

For more information see Stale Mocks.