mardi 29 octobre 2019

Unit testing WinForms forms

In unit testing .net winforms, we're coming across the need to manipulate and verify the state of various form controls, as well as to fire events on those controls. The problem is that the controls are private by default on the form, and the test method can't get at them.

So far, we've resorted to using magic strings to get the controls via reflection. This works, but it's brittle. Tests will break if the control name changes.

Changing all the controls to internal works, but it's a bit of a pain in that we have to remember to do it for each and every control we might want to access in a test.

I thought maybe I might use expression trees to get around the "privateness" of the control, but nothing obvious comes to mind as to how to go about building such code.

There's also the problem of raising events. So far, we're magic strings to hold event names, then using reflection to locate the OnXXX method and invoking it, but again, we'd like to do away with that.

Is there a way to use expression trees (or any other semantics or mechanisms) to access private controls, in order to manipulate or verify their state and fire their events?





Aucun commentaire:

Enregistrer un commentaire