views:

266

answers:

1

I'm facing a problem while unit testing my forms.

The problem is that data bindings are simply not working when the form is not visible.

Here's some example code:

Data = new Data();
EdtText.DataBindings.Add(
 new Binding("Text", Data, "Text", false, 
  DataSourceUpdateMode.OnPropertyChanged));

and later on:

Form2 f = new Form2();
f.Data.Text = "Test 1";
f.EdtText.Text = "Test 2";
f.Data.Text = "Test 3";

At the end the values for components are f.EdtText.Text = "Test 2" and f.Data.Text = "Test 3" but the expected values should be both "Test 3".

Any suggestions?

+2  A: 

I think you answered your own question -- in order for the property change event (TextChanged) to occur the control has to be displayed. Your unit test can just do something like this:

Form2 f = new Form2();
f.Show();
Thread.Sleep(2000); // give the Form time to open
f.Data.Text = "Test 1";
Assert.AreEqual("Test 1", f.EditText.Text);
f.Close();

Instead of exposing the Form components, you'll probably want to use NUnitForms to get the Form controls:

TextBoxTester tb = new TextBoxTester("EditText1");
Assert.AreEqual("Test 1", tb["Text"]);
Bob Nadler