首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Moq模拟CAL EventAggregator

使用Moq模拟CAL EventAggregator
EN

Stack Overflow用户
提问于 2009-03-12 14:38:12
回答 1查看 1.1K关注 0票数 3

我正在使用Composite Application Library的事件聚合器,并希望为IEventAggregator接口创建一个模拟,以便在我的单元测试中使用。

我计划使用Moq来完成这项任务,到目前为止,一个示例测试如下所示:

代码语言:javascript
复制
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方法中以这种方式定义所有事件,但我想知道是否有更聪明的方式?:-)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-03-18 09:04:43

我找到了这个问题的解决方案:

代码语言:javascript
复制
var mockEventAggregator = new Mock<IEventAggregator>{ DefaultValue = DefaultValue.Mock };

将使mockEventAggregator返回所有嵌套对象的模拟。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/638908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档