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