How to raise an event with NSubstitute with EventHandler<T>?

浪子不回头ぞ 提交于 2019-12-24 11:15:58

问题


I have an interface which defines multiple events, some with delegate type EventHandler<T> with T for example <string>. Objects implementing this interface are used in another class, a simple arrangement would look like this:

public interface IEventEmitter
{
    event EventHandler mySimpleEvent;
    event EventHandler<string> myStringEvent;
}

public class EventObserver
{
    public IEventEmitter Emitter;
    public int called = 0;
    public EventObserver(IEventEmitter emitter)
    {
        Emitter = emitter;

        Emitter.myStringEvent += (sender, text) =>
        {
            called++;
            Console.WriteLine("Observed event: {0}", text);
        };

        Emitter.mySimpleEvent += (sender, args) =>
        {
            called++; 
            Console.WriteLine("Observed event: simple");
        };
        Console.WriteLine("Registered observer");
    }
}

Now I want to test the code with NUnit3 and NSubstitute 3.1.0. With NSubstitute I can substitute these objects with a ...eh... substitute, and my code looks like this:

    [Test()]
    public void EventObserverTest()
    {
        // Arrange
        IEventEmitter emitter = Substitute.For<IEventEmitter>();
        var observer = new EventObserver(emitter);
        // Act
        emitter.mySimpleEvent += Raise.Event();
// the following line doesn't work
        emitter.myStringEvent += Raise.EventWith<EventHandler<string>>("Yeah");
        // Assert
        Assert.AreEqual(2, observer.called); 
    } 

Raising mySimpleEvent from the substitute works wonderful, but I am stuck and can't figure it out how to raise myStringEvent. (Actually, in the code I have to write it is not a string but a custom Type, but I boiled it down to this form).


回答1:


The Raise.EventWith<TEventArgs> syntax requires T to derive from EventArgs (see source).

To work with non-EventArgs and other delegate types we can use can use Raise.Event<TDelegate> as described in Raising Delegate events:

// Arrange
IEventEmitter emitter = Substitute.For<IEventEmitter>();
var observer = new EventObserver(emitter);
// Act
emitter.mySimpleEvent += Raise.Event();
emitter.myStringEvent += Raise.Event<EventHandler<string>>(this, "Yeah");
// Assert
Assert.AreEqual(2, observer.called);


来源:https://stackoverflow.com/questions/51930227/how-to-raise-an-event-with-nsubstitute-with-eventhandlert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!