3 added 22 characters in body
source | link

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?

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);
}

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?

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?

2 edited title
| link

Testing C# eventhandler and testingEventhandler Subscription

1
source | link

C# eventhandler and 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);
}

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?