我正在使用Composite Application Library的事件聚合器,并希望为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,...)定义一个空的mock?当然,我可以在TestInitialize方法中以这种方式定义所有事件,但我想知道是否有更聪明的方式?:-)
发布于 2009-03-18 09:04:43
我找到了这个问题的解决方案:
var mockEventAggregator = new Mock<IEventAggregator>{ DefaultValue = DefaultValue.Mock };将使mockEventAggregator返回所有嵌套对象的模拟。
https://stackoverflow.com/questions/638908
复制相似问题