Defining a Default Behavior for Static Methods

There is no need to create a fake instance in order to fake static methods.

When to Use

When your test requires a specific return value from the method.

Syntax
C#

Isolate.Fake.StaticMethods<Dependency>();

VB

Isolate.Fake.StaticMethods(Of Dependency)()

The following table explains possible behavior types:

 

Behavior

Description

ReturnRecursiveFakes

Returns:

      For reference types: fake objects

      For other return types: zero or equivalent


The returned fake objects will behave in the same way.

CallOriginal

Calls to fake static methods will pass through to their original implementation.

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.

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.

 

 

 Alternatively, you can use Isolate.WhenCalled().

Samples

The following sample shows how to test static methods.

 You can fake and verify chained static calls.

C#

[TestMethod]
public void FakeOneStaticMethod()
{
  Isolate.WhenCalled(()=>Dependency.CheckSecurity(null,null)).IgnoreCall();
 
  var result = new ClassUnderTest().Calculate(1, 2);
  Assert.AreEqual(3, result);
}
 
public int Calculate(int a, int b)
{
  Dependency.CheckSecurity("typemock", "rules");
  return a + b;
}

VB

<TestMethod()> 
Public Sub FakeOneStaticMethod()
  'Isolate.Fake.StaticMethods(Of Dependency)()
  Isolate.WhenCalled(Sub() Dependency.CheckSecurity(Nothing, Nothing)).IgnoreCall()


  Dim result = New ClassUnderTest().Calculate(1, 2)
  Assert.AreEqual(3, result)
End Sub