CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Ranjan Sakalley


Writing unit tests for Windows Forms to test DataBinding

Me and my pair were creating a form for the application, and basically testing out the data binding on the controls in the form.  Having  been bound to a domain object, the controls were tested as -

1. Create a domain object, and set the properties to be bound with random values.

2. Bind this/empty domain object to the windows form controls.

3. Set/Change the values in the controls(like Text property of a TextBox, depending on this being a create or edit form.

4. Assert the values of the current domain object state, with the values set/edited for equality.

Our library, to perform step 3, consists of methods like the following -

  void SetControlValue(string name, string controlProperty, object value);

which find out the control, then find out the mentioned property using reflection, and then set that property to the "value".

As we started writing the first test to check up the state of the first control, we found a major issue. Even after the 4 steps, the asserts were failing. We spent around 2-3 hours trying to find out if the data binding was done correctly or not, or if the repository methods that were setting the control values using reflection were doing it correctly. Just as we got up to ask for help, one of our collegues mentioned that -- A control's DataBinding does not update the values in the object bound to it until the focus on the control changes.

Why did I not notice it before writing my first test case? Having never written a unit test on any Windows Form, I was used to test my control bindings by checking the saved values etc. And that happened on the click of the "Save" button, which means the focus changed from the control to the button, and the data was saved to the object, and I could never notice this.

Man its fun to write tests.

Technorati :
Technorati Profile

Published Apr 16 2006, 04:23 PM by rsakalley
Filed under:

Comments

Adam Vandenberg said:

If you want to send the control's value back to the object without blurring the control first, an option is to retreive the Binding from the control and call Binding.WriteValue .
# April 16, 2006 5:43 PM

rsakalley said:

Thanks a lot Adam, that makes it more elegant for me to write

control.Databings<> . WriteValue();

than to write code to change the focus of the control.
# April 17, 2006 4:25 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Free Tech Publications