我有一个定义多个事件的接口,其中一些具有委托类型EventHandler<T>,T例如<string>. 实现这个接口的对象在另一个类中使用,一个简单的安排看起来像这样:
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");
}
}
现在我想用 NUnit3 和 NSubstitute 3.1.0 测试代码。使用 NSubstitute 我可以用 ...eh... 替代品来替代这些对象,我的代码如下所示:
[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);
}
提高mySimpleEvent从替代精彩的作品,但我被卡住,无法弄清楚如何提高myStringEvent。(其实在代码中我要写的不是字符串而是自定义Type,但是我把它归结为这种形式)。
ibeautiful
相关分类