C# – How to Test Eventhandler Subscription

ceventmockingnetunit testing

I want my unit tests to be able to verify that some events were subscribed or unsubscribed at specific points. Currently, I see two options.

VerifiableEventHandler

Each client subscribes using the common += and -= notation. Each class that has a verifiable eventhandler needs to implement IsSubscribed(), which can query the eventhandler using GetInvocationList().

public interface IVerifiableEventHandler<T> 
{
    event EventHandler<T> EventHandler;
    bool IsSubscribed(Delegate d);
}

// impl
public bool IsSubscribed(Delegate d)
{
    return EventHandler.GetInvocationList().Contains(d);
}

Custom subscription interface

The actual C# event handler would be hidden, and clients can only subscribe using the Subscribe / Unsubscribe function.

public interface IEventSource<T>
{
    void Subscribe(EventHandler<T> func);
    void Unsubscribe(EventHandler<T> func);
    void Raise(object sender, T event_args);
}

Pros and Contras

To me, the advantage of the first approach is that the principle of least surprise is not violated. A .Net programmer will expect events to be subscribed to using the += notation.

The advantage of the second approach is that it's very easy to test, as the functions are nothing special and can be verified easily if the class containing the eventhandler is mocked.

Is there a better way to solve this problem? If not, which of the two variants should be preferred, and why?

Best Answer

There's a third option: mock the object with the event and use behavior testing to verify the mock's subscriptions at given points. Several mocking frameworks allow for this.

public interface INeedToBeMocked
{
    public event EventHandler EventRaised;
}

NSubstitute

var mockedItem = Substitute.For<INeedToBeMocked>();
mockedItem.Received().EventRaised += Arg.Any<EventHandler>();

Rhino Mocks

var mockedItem = MockRepository.GenerateMock<INeedToBeMocked>();
mockedItem.AssertWasCalled(x => x.EventRaised += Arg.Is.Anything);
Related Topic