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 have a test that we were running successfully in 5.4.5 but fails in 6.0.4. The failing test fails during a Form initialization reporting "Unexpected Call to IsolatorFormLoadBug.Form1.add_Load()." The form, however, was not actually mocked, so I don't understand why the error is occurring.

I have reduced the testcase down to a very small test program containing only two identical tests. If I run both, the first will pass and the second will fail. If I run only the failing case, it will pass as well.

The test code:

        public void MyTestInitialize() 
        {
            MockManager.Init();
        }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        public void MyTestCleanup()
        {
            MockManager.Verify();
        }
        //
        #endregion

        [TestMethod]
        public void TestMethod1()
        {
            using (RecordExpectations recorder = CreateRecorder())
            {
                MockMe mockedObject = new MockMe();

                recorder.ExpectAndReturn(mockedObject.DoSomething(), "Mocked Value");
            }

            Form1 form = new Form1();

            IsolatorFormLoadBugTest.IsolatorFormLoadBug_Form1Accessor accessor = new IsolatorFormLoadBugTest.IsolatorFormLoadBug_Form1Accessor(form);

            using (RecordExpectations recorder = CreateRecorder())
            {
                recorder.ExpectAndReturn(accessor.ShowMessageBox("", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation), DialogResult.OK);
            }

            Assert.AreEqual("Mocked Value", form.Value);
        }
        [TestMethod]
        public void TestMethod2()
        {
            using (RecordExpectations recorder = CreateRecorder())
            {
                MockMe mockedObject = new MockMe();

                recorder.ExpectAndReturn(mockedObject.DoSomething(), "Next Mocked Value");
            }

            Form1 form = new Form1();

            IsolatorFormLoadBugTest.IsolatorFormLoadBug_Form1Accessor accessor = new IsolatorFormLoadBugTest.IsolatorFormLoadBug_Form1Accessor(form);

            using (RecordExpectations recorder = CreateRecorder())
            {
                recorder.ExpectAndReturn(accessor.ShowMessageBox("", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation), DialogResult.OK);
            }

            Assert.AreEqual("Next Mocked Value", form.Value);
        }
        private TypeMock.RecordExpectations CreateRecorder()
        {
            RecordExpectations recorder = RecorderManager.StartRecording();
            recorder.DefaultBehavior.AutomaticFieldMocking = false;

            return recorder;
        }


The project being tested is a simple form with the following implementation:

    public partial class Form1 : Form
    {
        private String _value;

        public String Value { get { return _value; } }

        public Form1()
        {
            InitializeComponent();

            MockMe mockedObject = new MockMe();

            _value = mockedObject.DoSomething();
        }

        // This is obviously linked to the form's load event
        private void Form1_Load(object sender, EventArgs e)
        {
            ShowMessageBox("arg1", "arg2", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
        }

        private DialogResult ShowMessageBox(string arg1, string arg2, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            return MessageBox.Show(arg1, arg2, buttons, icon);
        }
    }


The MockMe class is:

    public class MockMe
    {
        public string DoSomething()
        {
            return String.Empty;
        }
    }


Any idea why this is happening? I'm happy to send the entire testcase if necessary.

Thanks,
-Kevin
asked by kevinms99 (4.4k points)

5 Answers

0 votes
Hello Kevin,

Thanks for posting this. I'll check it out and let you know!
answered by igal (5.7k points)
0 votes
Hi Kevin,

Can you please send us the solution that reproduce the error.
I'll send you a mail from our support mail. Please reply to it and attach the solution.
answered by ohad (35.5k points)
0 votes
I have sent the testcase. Can you confirm receipt?
answered by kevinms99 (4.4k points)
0 votes
Hi Kevin,

We've received the file, we're investigating it.

Regards,
Elisha,
Typemock Support
answered by Elisha (12k points)
0 votes
Hi Kevin,

The reason that fails the test is missing cleanup between the tests. You can fix it using adding [ClearMocks] attribute to the tests.

[TestMethod]
[ClearMocks]
public void TestMethod1()
{
       // Test logic here...
}


In addition, assuming you're moving to the new Isolator version it's recommended to use the AAA API. You can gain tests which are much more readable and maintainable. Let us know if you need help moving to AAA API.

Regards,
Elisha,
Typemock Supprt
answered by Elisha (12k points)
...