Faking Instances of Objects
Fake object creation
Fake objects are objects that replace real objects in the code under
test. Fake objects behavior can be set up using Isolate.WhenCalled()
and verified using Isolate.Verify.
Fake objects replace Mock and MockObject in the AAA API.
Creating fake objects is done by using the Isolate.Fake.Instance()
method. This example shows basic fake object creation:
RealLogger fake =
Isolate.Fake.Instance<RealLogger>();
Fake object behaviors
Fake objects can be created with a few predefined behaviors. These
behavior types will set the default behavior for the created fake
object. Later you can replace specific method behavior by
using Isolate.WhenCalled().
The fake object behavior types are defined by passing a
Members
enum parameter to Isolate.Fake.Instance().
The possible behavior types are:
- Members.MustSpecifyReturnValues: Before using methods that
return a value on the fake object, their behavior must be defined. If a
method returning a value is called without first being set up, it will
throw an unexpected call exception. Void calls are ignored.
- Members.CallOriginal: The fake object will pass through any
calls made on it to their original implementation. The object's real
constructor is called during the fake object's creation. This is useful
when
it is desired to maintain the original behavior of a test object and
tweak only specific methods' behavior.
- 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.ReturnRecursiveFakes: 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.ReturnRecursiveFakes is the default fake object behavior.
This means that calling Isolate.Fake.Instance()
is the same as passing in Members.ReturnRecursiveFakes.
Recursive fake objects
Creating fake objects with Members.ReturnRecursiveFakes creates a
powerful and versatile faking tool. If the code under test calls an
intricate object model that is replaced by this fake object, deep calls
(chained calls several levels down the object model) will be
faked automatically. This saves the test author from faking
return values and defining
behavior for each hierarchy level explicitly.
The following example shows using recursive fake objects in order to
fake a deep call. The fake RealLogger we create calls GetSon() which in
turn returns another Son object on which another call is
made:
[TestMethod]
[Isolated]
public void
RecursiveStubWithExpectationOnSecondLevel ()
{
//
Arrange
RealLogger fake =
Isolate.Fake.Instance<RealLogger>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() =>
fake.GetSon().DoSomething()).WillReturn(10);
// Act
Logger son = fake.GetSon();
son.DoSomething();
//
Assert
Isolate.Verify.WasCalledWithAnyArguments(()
=> fake.GetSon().DoSomething());
}
See Also
Copyright © Typemock
Ltd.
2004-2010. All Rights Reserved.