IFakerStaticMethods Method (Type, Members)

Typemock Isolator Developer Guide
Overloaded. Fakes all static methods for the given type, 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

void StaticMethods(
	Type type,
	Members behavior
)

Parameters

type
Type: SystemType
The type to fake static methods for.
behavior
Type: TypeMock.ArrangeActAssertMembers
Can be one of the following values:
Members ValueDescription
Members.MustSpecifyReturnValuesDefault. All 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.ReturnRecursiveFakesAll 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.

Return Value

Type: 
A fake instance of Type T.
Remarks

This overload receives a Type parameter representing the class to fake static methods for, and should be used when faking methods for static classes. For other non-static classes it is generally recommended to use the generic overload StaticMethodsT(Members), which receives the class as a generic type argument.
Examples

The following test shows the default behavior of FakeStaticMethods():
C#
[TestMethod]
[Isolated]
public void FakeStaticMethods_UseDefault()
{
    Isolate.Fake.StaticMethods(typeof (LoggerFactory));

    LoggerFactory.Count = 0;
    // This call is faked because we used the default overload for FakeStaticMethods - 
    // IncrementCount() is not really performed
    LoggerFactory.IncrementCount();

    Assert.AreEqual(0, LoggerFactory.Count);

    // The following statement will throw, because we didn't set behavior on GetCount() method.
    int x = LoggerFactory.GetCount();
}
The next test shows the CallOriginal setting for FakeStaticMethods():
C#
[TestMethod]
[Isolated]
public void FakeStaticMethods_UseCallOriginal()
{
    Isolate.Fake.StaticMethods(typeof (LoggerFactory), Members.CallOriginal);

    LoggerFactory.Count = 0;

    // This call is not faked. Count should be incremented.
    LoggerFactory.Increment();
    Assert.AreEqual(1, fake.Count);
}
The next test shows what happens when using ReturnNulls:
C#
[TestMethod]
[Isolated]
public void FakeStaticMethods_UseReturnNulls()
{
    Isolate.Fake.StaticMethods(typeof (LoggerFactory), Members.ReturnNulls);

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

    // Since we're returning Null by default the following Assert passes
    Assert.IsNull(LoggerFactory.GetLogger());
}
Finally, using recursive faking, this test should pass:
C#
[TestMethod]
[Isolated]
public void RecursiveStubsChain()
{
     Isolate.Fake.StaticMethods(typeof (LoggerFactory), Members.ReturnRecursiveFakes);

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

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

     // And the result of GetLogger also returns a non-null object 
     Assert.IsNotNull(LoggerFactory.GetLogger().GetWriter());
}
See Also

Reference