chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I've encounterred a very strange problem and I'm not sure whether it's a bug.


[TestMethod]
public void WindowNatualMockTest()
{
using (RecordExpectations r = RecordManager.StartRecording())
{
System.Windows.Window win = new System.Windows.Window();//[1]
}
}

When I run it the following error occurred at location [1].
Error Message:
System.Reflection.TargetInvocationException: --->
System.TypeInitializationException: --->
System.Window.FrameworkElement: --->
System.TypeInitializationException:
Error Stack Trace:
System.Windows.Freezable.Freeze(Boolean isChecking)
System.Windows.Freezable.Freeze()
System.Windows.Media.Transform.MakeIdentityTransform()
System.Windows.Media.Transform..cctor()
System.Windows.Media.Transform.get_Identity()
System.Windows.FrameworkElement..cctor()
System.RuntimeFieldHandle.GetValue(Object instance, RuntimeTypeHandle fieldType, RuntimeTypeHandle declaringType, Boolean& domainInitialized)
System.Reflection.RtFieldInfo.InternalGetValue(Object obj, Boolean doVisibilityCheck, Boolean doCheckConsistency)
System.Reflection.RtFieldInfo.InternalGetValue(Object obj, Boolean doVisibilityCheck)
System.Reflection.RtFieldInfo.GetValue(Object obj)
TypeMock.Mock.a(Object A_0)
TypeMock.Mock.d(Object A_0)
TypeMock.RecorderManager.a(String A_0, String A_1, Object A_2, Type A_3, Boolean A_4)
TypeMock.RecorderManager.a(String A_0, String A_1, Object A_2, Object[] A_3, MethodBase A_4, Object A_5, StackTrace A_6)
TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
System.Windows.Window..ctor()

I tried using the Reflective Mock and mock successfuly.

[TestMethod]
public void WindowReflectiveMockTest()
{
MockManager.Init();
System.Windows.Window win = MockManager.MockObject<System>().MockedInstance;
//...
MockManager.Verify();
}

Environment as follow:
<<<<Microsoft>>>>

Any help will be great appreciate.

chris
asked by (1.1k points)

1 Answer

0 votes
Hi Chris,

You've entered the realms of uninitialized fields. The default when using Natural mocks is to have fields mocked automatically. But is sometimes creates problems. Like in your case.

Try the following:

using (RecordExpectations rec = RecorderManager.StartRecording())
{
     rec.DefaultBehavior.AutomaticFieldMocking = false;
     System.Windows.Window window= new System.Windows.Window();
}


It should solve your problem.

Please click the following link for more information on
Natural Mocks


Let me know how it went.
answered by (14.5k points)
...