嘲讽CAL EventAggregator与起订量
-
11-07-2019 - |
题
我使用的是的复合应用程序库事件聚集,想创建一个模拟为 IEventAggregator 接口,其用于我的单元测试。
我打算在使用 Moq的执行此任务,和示例测试,以便远远看上去是这样的:
var mockEventAggregator = new Mock<IEventAggregator>();
var mockImportantEvent = new Mock<ImportantEvent>();
mockEventAggregator.Setup(e => e.GetEvent<SomeOtherEvent>()).Returns(new Mock<SomeOtherEvent>().Object);
mockEventAggregator.Setup(e => e.GetEvent<SomeThirdEvent>()).Returns(new Mock<SomeThirdEvent>().Object);
// ...
mockEventAggregator.Setup(e => e.GetEvent<ImportantEvent>()).Returns(mockImportantEvent.Object);
mockImportantEvent.Setup(e => e.Publish(It.IsAny<ImportantEventArgs>()));
// ...Actual test...
mockImportantEvent.VerifyAll();
这工作得很好,但我想知道,如果有一些聪明的办法,以避免必须定义一个空模拟每一个事件型我的代码可能会遇到(SomeOtherEvent,SomeThirdEvent,...)?我当然可以定义我所有的事件,这样在[TestInitialize]的方法,但我想知道是否有一个更聪明的方法? : - )
解决方案
我发现这个解决方案:
var mockEventAggregator = new Mock<IEventAggregator>{ DefaultValue = DefaultValue.Mock };
将使mockEventAggregator返回嘲笑所有嵌套的对象。
不隶属于 StackOverflow