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
0 votes
I've encounterred a very strange problem and I'm not sure whether it's a bug.

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: --->
Error Stack Trace:
System.Windows.Freezable.Freeze(Boolean isChecking)
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)

I tried using the Reflective Mock and mock successfuly.

public void WindowReflectiveMockTest()
System.Windows.Window win = MockManager.MockObject<System>().MockedInstance;

Environment as follow:

Any help will be great appreciate.

asked by won.wingcrown@gmail. (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 gilz (14.5k points)