Test that an event has been wired to a handler?

I have a class that exposes a public event named StatusChanged.


public event StatusChangedDelegate StatusChanged;
 

The class that creates this class is supposed to wire this event to appropriate handlers. I'm trying to write a simple test that verifies this has occurred. However, the following line in a test:

Assert.IsTrue(target.StatusChanged != null);
 

returns this error: "Error 8 The event 'NewDawnTechControls.NdtSessionController.StatusChanged' can only appear on the left hand side of += or -=."

Inside my class (with the event) I have an intercept method that is the one that actually raises the event, and in that method I have nearly the exact same statement:

if (StatusChanged != null)

{

StatusChanged(this, new StatusChangedEventArgs(message, status));

}
 


It works inside the class with the event, but not from outside. From outside the class, how can I verify the event has been wired

Thanks.



Answer this question

Test that an event has been wired to a handler?

  • Katou

    I may not have a full understanding of delegates and events, et al. But I understand an instance of a delegate to be essentially a function pointer. So from within a class, if I want to "make a call" that is "heard" by more than one external class/method I cannot do that with a single delegate call. I could possibly have multiple delegates registered, and call them all sequentially, but that requires tighter coupling and more code. Maybe an array of delegates that have been registered with my class

    Ugh!

    Events are made to do this, no Requiring coupling only to the degree that the listeners need to know about an interface that exposes the event so they can register for it, or using an intermediary EventBroker class, or whatever. The event-generating class only needs to know to say RaiseMyEvent() and whoever is registered to handle the event will get the call, regardless if there is only one, or one thousand.

    My original query was just about seeing from outside the event-generating class if an event had been registered for yet. It is just a small point to try to have more thorough unit testing.

  • vikassony

     Michael Koltachev wrote:
    Hello!

    If you want to check just the assigntment from outside, remove the word 'event' from your event variable declaration so that it becomes: public StatusChangedDelegate StatusChanged. After that the code should compile fine.

    Thanks,
    Michael

    Since we need to wire multiple handlers to this event, we cannot use a delegate for this. I suppose I'm trying to test something that I can't really test.

    Thanks.

  • Konstantin Triger

     Michael Koltachev wrote:
    What is the problem with wiring up multiple handlers to the delegate  If you get a compile error can you attach the code
    I do not know how to do this. Do you have a small code sample that shows how to do it

    Thanks.

  • Ewa

    Hello! Well, in general delegates are more powerful than events and you have more control when using delegates. What is the problem with wiring up multiple handlers to the delegate  If you get a compile error can you attach the code

    Thanks,
    Michael

  • GeoffMF

    Hello!

    Well, in general this compiler error is correct .net behavior. As far as I understand how events are usually used is that the event is fired from inside the class that holds the event variable. For instance, consider a Button. You click on it in UI, then the Button class inside sees if there is a event registered (check for null) and if is, fire the event. Or there can be a method OnClick() that can be called from outside the class, it would check the event, etc. Delegates do not the limitation that you can't check for null from outside the class.

    Also, if you want to assert whether the event was actually fired, i.e. be100% sure (checking for null means that event was just assigned, not necesserily fired) you may set some variable in the actualy event receiver method.

    If you want to check just the assigntment from outside, remove the word 'event' from your event variable declaration so that it becomes: public StatusChangedDelegate StatusChanged. After that the code should compile fine.

    Thanks,
    Michael

  • Test that an event has been wired to a handler?