When isolating a static method of a class, the instance constructor is no longer run when called from static member initialization or static constructor of the same class. Given this simple class:
public class CrazySingleton
{
public static CrazySingleton Instance = new CrazySingleton();
public CrazySingleton() { throw new Exception("Instance constructor says 'You shall not pass!'"); }
public static int Echo(int num) { return num; }
}
And given the following test class:
[TestClass, Isolated] public class CrazySignletonTests
{
[TestMethod] public void Test1()
{
CrazySingleton.Echo(7);
}
[TestMethod] public void Test2()
{
Isolate.WhenCalled(() => CrazySingleton.Echo(0)).WillReturn(7);
CrazySingleton.Echo(7);
}
}
When you run the first test by itself, it fails with a TypeInitializationException as you'd expect. When you run the second test by itself, it passes, though it shouldn't - it should throw the same TypeInitializationException as the first test.
My problem is that in the singleton class that I'm trying to test, the static constructor accesses a static method, which I want to isolate, and then constucts the singleton instance. Unfortunatly, if I fake the static method, the instance constructor no longer runs. I have been unable to find a workaround.
I'm using Isolator 6.2.5.0 with Visual Studio 2010 SP1 10.0.40219.1 SP1Rel and .NET Framework 4.0.30319 SP1Rel on Windows 7 Professional x64 SP1 (build 7601).
Thanks!