samedi 5 décembre 2015

How to check for valid Event Handlers from a unit test (or even from the same unit)

I have found several times that for some reason (for instance, Copy&Paste in the form designer), the Event Handlers assigned to some of the components of a form get lost, i.e., the events are not connected to the correct function in the code of the form.

I would like to check this from either the form code, or from some unit test: ensure that they are not empty at least, or check that they are connected to the correct event handler.

I have seen the questions regarding this, but I can't pass the error The event 'SomeEvent' can only appear on the left hand side of += or -=. This happens when trying to compare it to null, or even as some other answers suggest, get the list of delegates and then check those.

I'm not trying to mess with the events. On the contrary, I want to ensure that they are correctly set from my unit tests.

Manually subscribing the event handlers to the events of the components in the constructor, for instance, seems like a bad idea to me. They should already be subscribed in the InitializeComponent() code... and checking if they are or not to subscribe them is exactly the problem I want to solve.

Aucun commentaire:

Enregistrer un commentaire