I tried to mock Windows Form showDialog() calls, something like
TestForm test= new TestForm("abcd");
Isolate.Swap.NextInstance<TestForm>().With(test);
Isolate.WhenCalled(() => test.ShowDialog()).WillReturn(System.Windows.Forms.DialogResult.Abort);
but get this exception:
TestCase 'UnitTests.Test_OKToContinue_NotOK'
failed: System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
----> System.TypeInitializationException : The type initializer for 'System.Windows.Forms.Form' threw an exception.
----> TypeMock.TypeMockException :
*** Faking behavior on struct System.Windows.Forms.Padding is not supported in the following cases:
1. Faking behavior on more than one struct of the same type.
2. Faking behavior on more than one method on the same struct.
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at ga.c()
at da.a(Boolean A_0)
at de.b(Boolean A_0)
at il.c(Boolean A_0)
at il.c(Object A_0)
at TypeMock.ArrangeActAssert.ExpectationEngine`1.a(TResult A_0)
TestCodeUnitTestsTest1.cs(146,0): at UnitTests.Test_OKToContinue_NotOK()
--TypeInitializationException
at System.Windows.Forms.Form.ShowDialog()
TestCodeUnitTestsTest1.cs(146,0): at UnitTests.<>c__DisplayClass1c.<Test_OKToContinue_NotOK>b__19()
--TypeMockException
at f3.a(Type A_0, Object A_1, MethodBase A_2, String A_3, TypeParams A_4)
at f3.a(Object A_0, String A_1, String A_2, MethodBase A_3, Object[] A_4, Object A_5)
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 System.Windows.Forms.Padding..ctor(Int32 all)
at System.Windows.Forms.Form..cctor()
Could someone explain this exception and what might be causing this? Specifically, I'm trying to understand what the following means and how to overcome this error:
*** Faking behavior on struct System.Windows.Forms.Padding is not supported in the following cases:
1. Faking behavior on more than one struct of the same type.
2. Faking behavior on more than one method on the same struct.
Again, I'm swapping the TextForm instance using: Isolate.Swap.NextInstance<TestForm>().With(test);
but when the test executes, it creates a new object again instead of swapping. Is that causing the exception? What am I doing wrong?
I have several such tests in the same TestClass. When these tests are run individually, they pass. But when I run all tests in the class, all of them fail.
I may be overlooking some necessary setting.
Any help is greatly appreciated.
Thanks
Merrin