Hi, I have a windows Forms which has a button(named button1) and 2 textboxes(named textbox1 and textbox2) on it. I'll supply the code of my form and my 2 test functions below. The problem is that the results of test methods are different when debugging by using F10 form debugging by using F11. To clarify:
My form:
public partial Class Form1: Form
{
public Form()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if(textbox1.Text == textbox2.Text)
{
this.Close();
}
else
{
MessageBox.Show("Not closing the form", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
}
}
}
I am trying to write test for button1_Click function.
My test methods:
[TestMethod()]
public void button1_ClickTest1()
{
Form1 form1 = Isolate.Fake.Instance<Form1>(Members.CallOriginal);
TextBox textBox1 = ObjectState.GetField(form1, "textBox1") as TextBox;
TextBox textBox2 = ObjectState.GetField(form1, "textBox2") as TextBox;
Isolate.WhenCalled(() => form1.Close()).IgnoreCall();
textBox1.Text = "asdf";
textBox2.Text = "asdf";
Button realButton = ObjectState.GetField(form1, "button1") as Button;
Isolate.Invoke.Event(() => realButton .Click += null, realButton, new EventArgs());
Isolate.Verify.WasCalledWithAnyArguments(() => form1.Close()); //*
}
[TestMethod()]
public void button1_ClickTest2()
{
Form1 form1 = Isolate.Fake.Instance<Form1>(Members.CallOriginal);
TextBox textBox1 = ObjectState.GetField(form1, "textBox1") as TextBox;
TextBox textBox2 = ObjectState.GetField(form1, "textBox2") as TextBox;
Isolate.WhenCalled(() => MessageBox.Show("Not closing the form", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0)).WillReturn(DialogResult.OK);
textBox1.Text = "asdf";
textBox2.Text = "gfdsa";
Button realButton = ObjectState.GetField(form1, "button1") as Button;
Isolate.Invoke.Event(() => realButton .Click += null, realButton, new EventArgs()); //**
Isolate.Verify.WasCalledWithAnyArguments(() => MessageBox.Show("Not closing the form", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0));
}
Now when I ran the test it falied on line marked as "**" with error "ArgumentException was unhandled by user code. An item with the same key has already been added.". So I decided to debug on my test methods. When I use only F10 key during debug, I got the same error message on the same line as when I ran the test. But when use F11 key during debug I got "NestedCallException was unhandled by user code. Verify does not support using a property call as an argument. -To fix this pass null instead of ButtonBase.Text." error on the line marked as "*".
Why does any difference occur during debugging, and what should I do to test if close function of form called?
Thanx