IFakerInstanceT Method (Members)

Typemock Isolator Developer Guide
Overloaded. Returns a fake instance of type T, and allows to set the behavior of how the fake object is created.

Namespace:  TypeMock.ArrangeActAssert.Fluent
Assembly:  Typemock.ArrangeActAssert (in Typemock.ArrangeActAssert.dll) Version: 9.4.1.0 (9.4.1.0)
Syntax

T Instance<T>(
	Members behavior
)

Parameters

behavior
Type: TypeMock.ArrangeActAssertMembers
Can be one of the following values:
Members ValueDescription
Members.MustSpecifyReturnValuesAll void calls are ignored. Unless using WhenCalled(Action) on methods that return values and properties, they will throw a .
Members.ReturnNullsAll void calls are ignored. Unless using WhenCalled(Action) on methods that return values and properties, they will return null values (or zero for value types).
Members.CallOriginalAll methods are called. Use WhenCalled(Action) to change this behavior.
Members.ReturnRecursiveFakesDefault. All void calls are ignored. When calling the fake's methods returning values or properties, they will return a fake value, which presents this behavior recursively. Any deep call will never return a Null value.

Type Parameters

T
The type of the fake instance to be created.

Return Value

Type: T
A fake instance of Type T.
Remarks

Examples

The following test shows the faking an instance using Members.MustSpecifyReturnValue:
[TestMethod]
[Isolated]
public void FakeAnInstance_MustSpecifyReturnValues()
{
    RealLogger fake = Isolate.Fake.Instance<RealLogger>(Members.MustSpecifyReturnValues);

    fake.Count = 0;

    // This call is faked because we used MustSpecifyReturnValues - count should not increment
    fake.Increment();
    Assert.AreEqual(0, fake.count);

    // The following statement will throw, because we didn't set behavior on IntCall method.
    int x = fake.IntCall();
}
The next test shows the CallOriginal setting on Instance:
[TestMethod]
[Isolated]
public void FakeAnInstance_UseCallOriginal()
{
    RealLogger fake = Isolate.Fake.Instance<RealLogger>(Members.CallOriginal);

    fake.Count = 0;

    // This call is not faked. Count should be incremented.
    fake.Increment();
    Assert.AreEqual(1, fake.Count);
}
The next test shows what happens when using ReturnNulls:
[TestMethod]
[Isolated]
public void FakeAnInstance_UseReturnNulls()
{
    RealLogger fake = Isolate.Fake.Instance<RealLogger>(Members.ReturnNulls);

    fake.Count = 0;
    // This call is faked because we set faked method behavior using ReturnNulls 
    fake.Increment();
    Assert.AreEqual(0, fake.Count);

    // Since we're returning Null by default the following Assert passes
    Assert.IsNull(fake.IntCall());
}
Finally, using recursive faking, this test should pass:
[TestMethod]
[Isolated]
public void RecursiveStubsChain()
{
     RealLogger fake = Isolate.Fake.Instance<RealLogger>(Members.ReturnRecursiveFakes);

     // This method returns a value type, and therefore, returns a fake 0.
     Assert.AreEqual(0, fake.ReturnFive());

     // This method returns an object, and therefore returns a non-null object
     Assert.IsNotNull(fake.GetSon());

     // And the result of GetSon also returns a non-null object 
     Assert.IsNotNull(fake.GetSon().GetNephew());
}
See Also

Reference