Hi All,
I'm relatively new to the TypeMock scene and I am having an issue Isolating a call to a static function in a class that has a static constructor. I'm fairly certain it's due to the static c'tor. The error I get is as follows, the particularly interesting spots are:
TypeMock.CannotEvaluateWhileRecordingException:
*** Cannot evaluate methods inside a recording block
and a little bit further down:
System.TypeInitializationException: The type initializer for 'Comkt.Web.Estara.EstaraHelper' threw an exception.
at Comkt.Web.Estara.EstaraHelper.GetCoreJS(EstaraPageCategory templateCategory)
TypeMock.CannotEvaluateWhileRecordingException:
*** Cannot evaluate methods inside a recording block
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected, Object p1)
at NetUtils.Legacy.LegacyUtil.GetConfig(String configName) in Util.cs: line 13
at Comkt.Web.Estara.EstaraHelper..cctor() in EstaraHelper.cs: line 35
System.TypeInitializationException: The type initializer for 'Comkt.Web.Estara.EstaraHelper' threw an exception.
at Comkt.Web.Estara.EstaraHelper.GetCoreJS(EstaraPageCategory templateCategory)
at CMWebTests.Estara.BaseEstaraControllerTests.<GetEstaraCoreScript_Test>b__0() in BaseEstaraControllerTests.cs: line 89
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
at CMWebTests.Estara.BaseEstaraControllerTests.GetEstaraCoreScript_Test() in BaseEstaraControllerTests.cs: line 78
Pertinents parts of class I'd like to mock calls to/from:
public class EstaraHelper {
private static bool estaraIntegrationOn;
private static string coreScriptUrl;
private static string accountId;
private static string buttonImageUrl;
private static bool EstaraIntegrationOn {
get { return estaraIntegrationOn; }
}
private static string CoreScriptUrl {
get { return coreScriptUrl; }
}
private static string AccountId {
get { return accountId; }
}
private static string ButtonImageUrl {
get { return buttonImageUrl; }
}
static EstaraHelper() {
string estaraIntegrationSetting = LegacyUtil.GetConfig("EstaraIntegrationOn");
estaraIntegrationOn = estaraIntegrationSetting.ToLower().Equals("y");
accountId = LegacyUtil.GetRequiredConfig("EstaraAccountId");
coreScriptUrl = LegacyUtil.GetRequiredConfig("EstaraCoreScriptURL");
buttonImageUrl = LegacyUtil.GetRequiredConfig("EstaraPttButtonUrl");
}
...
//FUNC I'D LIKE TO FORCE THE RESULTS FROM
public static string GetCoreJS(EstaraPageCategory templateCategory) {
if (EstaraIntegrationOn) {
return PutTogetherCoreJS(templateCategory);
}
else {
return String.Empty;
}
}
}
A simple NUnit test causing the error:
[Test, Isolated]
public void GetEstaraCoreScript_Test() {
const string testCoreJsScript = "My Test Core JS Script";
const long testPageViewEventIt = 9999;
const EstaraPageCategory testPageCategory = EstaraPageCategory.AutoAspx;
MockEstaraController mockEstaraController = new MockEstaraController(testPageCategory, testPageViewEventIt);
Isolate.WhenCalled(() => EstaraHelper.GetCoreJS(EstaraPageCategory.AutoAspx)).WillReturn(testCoreJsScript);
//function under test
string result = mockEstaraController.GetEstaraCoreScript();
Assert.AreEqual(testCoreJsScript, result);
}
The static c'tor in question is relatively benign, just setting some private class variables from the configuration system, I could change the public accessors to them to lazy initialize, but I'd rather not have to change the class under test.
Any thoughts?
Thanks in advance for the help!