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
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
asked by mkg (4.3k points)

2 Answers

0 votes
Hi,

I run the tests you posted here and it passed.
Can you please tell me what version of the Isolator are you using?
answered by ohad (35.4k points)
0 votes
Hi,
I am using version 6.2.5. I realized that I forgot to add "Isolated" attribute to my test class. When I added attribute the tests actually pass. But then I put break points to the start of 2 test methods and debug through test methods. When I use F10 key during debug no problem occurred. But when I used F11 key I got an NestedCallException.

[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()); //*
}


NestedCallException occurred on line marked as * and written "NestedCallException was unhandled by user code. Verify doesn't support using a property call as an argument. -To fix this pass null instead of ButtonBase.Text" I wonder why the difference occurs.

Thanks.
answered by mkg (4.3k points)
...