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 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
asked by mkurian (1.8k points)

8 Answers

0 votes
hi,
first thing to make sure when tests starts to mess with one another is that you clean up your fakes between tests.

normally this is done using the [Isolated] on the test class (or every test method involved)

another thing to try is to use Isolate.Fake.Instance<TestForm>() to create your fake, instead of actually creating a real instance.

from your call stack it seems that you are failing somewhere inside the static constructor of System.Windows.Forms.Form. I cant really say why.

:idea: it might help if you can post the rest of the code (along with the relevant parts of the code under test)
answered by error (6.6k points)
0 votes
Using the fake instance was throwing some exception, so I resorted to using real instance.

I see that there is a known issue: "Automatic calling of the static constructor will not work before static field access only with method access. " Is it related?

Has it got something to do with 'not being able to mock core libraries' ? Should I try another framework to mock Windows Forms then?

Thanks
Merrin
answered by mkurian (1.8k points)
0 votes
hi,

The thing that seems weird is that the tests are passing alone but fails when run together.
in most cases this means that some faking is leaking between tests.
it seems to be related to the activation of the cctor. so my guess would be that somewhere you have faked the cctor and it gets executed in the wrong place.

The things you mention does not seem to be related.

on my side its really hard to tell what exactly goes on without more info.
you can try posting some of the code here. or maybe contact typemock support directly if thats not an option
answered by error (6.6k points)
0 votes
Merrin,

Isolator is able to fake windows forms behavior, so that should not be the problem. As Lior said if you can post the relevant code here we could find out if there's some bug or if you missed something.

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Thank you so much for your replies.

When I try to use Isolate.Fake.NewInstance<> to fake the TestForm, it throws the same exception as I mentioned before. So I think the problem is there itself. I have sent an email to support with a little more details.

Merrin
answered by mkurian (1.8k points)
0 votes
I created a new independent project without any of my code and wrote the following:

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using System.Windows.Forms;
using TypeMock.ArrangeActAssert;

namespace TestProject2
{
[TestFixture]
public class TestClass
{
[Test]
public void Test()
{
Form1 testFrom = Isolate.Fake.Instance<Form1>();
Isolate.Swap.NextInstance<Form1>().With(testFrom);
Isolate.WhenCalled(() => testFrom.ShowDialog()).WillReturn(DialogResult.OK);
Foo foo = new Foo();
Assert.AreEqual(true, foo.Bar());
}
}
public class Foo
{
public bool Bar()
{
Form1 f = new Form1();
DialogResult result = f.ShowDialog();
if (result.Equals(DialogResult.OK))
return true;
else
return false;
}
}
}



This also threw the same exception as I had mentioned earlier:

Error 1 TestCase 'TestProject2.TestClass.Test'
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)
at TestProject2.TestClass.Test() in D:p4TestProject2UnitTest1.cs:line 19
--TypeInitializationException
at System.Windows.Forms.Form.ShowDialog()
at TestProject2.TestClass.<>c__DisplayClass1.<Test>b__0() in D:p4TestProject2UnitTest1.cs:line 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() D:p4TestProject2UnitTest1.cs 19
answered by mkurian (1.8k points)
0 votes
This is unfortunately a bug in isolator. It's caused by mishandling static constructors while recording. We will work to fix this issue in a future version.

In the meanwhile and as a workaround you can fake the form's static constructor by adding this to the beginning of the test:
Isolate.Fake.StaticConstructor<Form1>();


Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Have you resolved this issue in Isolator? if yes, which version has this issue address?

I am not ble to resole this issue as i am using structure not static class or contructor.


Mustafa
answered by mushy1 (140 points)
...