I am unit testing SharePoint code that calls methods of the SPUtility class. My tests fail because the SPUtility class's static ctor throws a null reference exception, even though I have mocked the static ctor. Here is a minimal repro, using a static class of my own instead of SPUtility (just in case there was something odd about the SPUtility class):
namespace TypeMockTest
{
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TypeMock.ArrangeActAssert;
public static class MyStaticClass
{
static MyStaticClass()
{
throw new Exception("MyStaticClass cctor should not be called.");
}
public static string Foo()
{
return "Foo";
}
}
[TestClass]
public class StaticConstructorFailure
{
[TestMethod, Isolated]
public void StaticConstructorFails()
{
Isolate.Fake.StaticConstructor(typeof(MyStaticClass));
Isolate.Fake.StaticMethods(typeof(MyStaticClass));
Isolate.WhenCalled(() => MyStaticClass.Foo()).WillReturn("Bar");
}
}
}
This test fails because the "Isolate.WhenCalled" line throws the exception from the static ctor, even though the static ctor is mocked.
Thanks,
Larry