Invoke Static Constructors
Because a static constructor for a type is executed only once, when you fake a static constructor, you need to invoke it in a test that requires a normal execution. While Typemock Isolator does this automatically, you can force a static constructor call.
When to Use
When you want to make sure that the type is reinitialized (for example, when testing a singleton and making sure the instance is null).
Syntax
C# Isolate.Invoke.StaticConstructor(typeof(<type>));
VB
Isolate.Invoke.StaticConstructor(GetType(<type>))
Samples
C# [TestMethod, Isolated] public void CallingStaticConstructorTest() { StaticConstructorExample.TrueOnStaticConstructor = false; // force static constructor to be called Isolate.Invoke.StaticConstructor(typeof(<StaticConstructorExample>)); Assert.IsTrue(StaticConstructorExample.TrueOnStaticConstructor); } public class StaticConstructorExample { private static bool trueOnStaticConstructor = false; public static bool TrueOnStaticConstructor { get { return trueOnStaticConstructor; } set { trueOnStaticConstructor = value; } } static StaticConstructorExample() { trueOnStaticConstructor = true; } }
VB
<TestMethod, Isolated>
Public Sub CallingStaticConstructorTest()
StaticConstructorExample.TrueOnStaticConstructor = False
'force static constructor to be called
Isolate.Invoke.StaticConstructor(GetType(StaticConstructorExample))
Assert.IsTrue(StaticConstructorExample.TrueOnStaticConstructor)
End Sub
Public Class StaticConstructorExample
Private Shared m_trueOnStaticConstructor As Boolean = False
Public Shared Property TrueOnStaticConstructor() As Boolean
Get
Return m_trueOnStaticConstructor
End Get
Set
m_trueOnStaticConstructor = Value
End Set
End Property
Shared Sub New()
m_trueOnStaticConstructor = True
End Sub
End Class