Controlling Live Objects Behavior
Faking Behavior for Live Objects
A live object is a test object which has been instantiated normally,
and not through the AAA mechanisms (see creating fake objects).
When it's only desired to modify a test object's behavior for a
specific method call, it is useful to use a live object and alter
only the
specific behavior rather than create a new fake object using
Isolate.Fake.Instance().
Example:
RealLogger logger = new
RealLogger(); // this is a normal "live" object
Isolate.WhenCalled(() => logger.Log("message")).IgnoreCall(); //
ignore only the Log method
When altering behavior for live objects, only the method(s)
being
changed will be impacted. All other methods will continue to return
their original implementations. This is similar to creating a fake
object using the Members.CallOriginal setting.
The same principals apply to changing behavior for specific static
methods without first using Isolate.Fake.StaticMethods (see faking static method behavior).
When a static method for a type is being altered like this, all other
static methods will continue to work normally.
Example:
Isolate.WhenCalled(()
=> LoggerFactory.GetLogger()).WillThrow(new
OutOfMemoryException());
live
objects and static behavior faking work also for non-public calls (see non-public faking)
through the Isolate.NonPublic.WhenCalled() entry point for methods,
Isolate.NonPublic.Property for properties and Isolate.NonPublic.Indexer
for index calls.
Example:
RealLogger logger = new
RealLogger();
Isolate.NonPublic.Property.WhenGetCalled(logger,
"IsDiskFull").WillReturn(true); // faking internal property behavior on
real
object
Verifying Live Objects Behavior
Live objects support verification just like fake objects (see verifying behavior)
with one limitation: it is only possible to verify calls on objects or
types (in the case of static methods) which were used
(activated) with WhenCalled()
Example:
[TestMethod]
public void Test1()
{
RealLogger logger = new RealLogger();
logger.Write("Hello");
Isolate.Verify.WasCalledWithAnyArguments(() =>
logger.Write("")); // this verification fails - the live object did not
pass through WhenCalled()
}
[TestMethod]
public void Test2()
{
RealLogger logger = new RealLogger();
Isolate.WhenCalled(()
=> logger.Write("")).IgnoreCall();
logger.Write("Hello");
Isolate.Verify.WasCalledWithAnyArguments(() =>
logger.Write("")); //
this verification succeed - the live object was 'activated' in the bold
line
}
Copyright © Typemock
Ltd.
2004-2010. All Rights Reserved.