FBryant87
FBryant87

Reputation: 4595

Nsubstitute - Raise event of mocked object when handler uses generics

We mock an interface which has an event on it like so:

public interface IThing<TKey, TValue>
{
    event EventHandler<Message<TKey, TValue>> OnMessage;
}

Using NSubstitue we make a mock of the interface and try to raise the event using NSubstitute guidelines:

var mockThing = Substitute.For<IThing>();

mockThing.OnMessage += Raise.EventWith(???)

Despite many attempts, we can't get that line to compile - is it just a case of placing the generic types and arguments in the correct position?

The actual OnMessage() function called will look like this:

private void OnMessage(object sender, Message<string, string> message)

Upvotes: 1

Views: 1041

Answers (1)

lars k.
lars k.

Reputation: 592

It should be something like

mockThing.OnMessage += Raise.Event<EventHandler<Message<string, string>>>(this, new Message<string, string>("yes", "what"))

Upvotes: 3

Related Questions