为什么我的Moq IEventAggregator验证失败? [英] Why does my Moq IEventAggregator verification fail?

查看:64
本文介绍了为什么我的Moq IEventAggregator验证失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Composite WPF(Prism),并且试图对我的控制器确实订阅了Composite Event进行单元测试.

I use Composite WPF(Prism) and I am trying to unit test that my Controller does in fact subscribe to a Composite Event.

我的订阅代码如下...

My subscription code looks as follows...

//Init Events.
this.eventAggregator.GetEvent<PlantTreeNodeSelectedEvent>().Subscribe(
    ShowNodeDetails, ThreadOption.UIThread);

我的单元测试代码如下(我使用Moq作为我的Mocking框架,使用Unity作为我的DI框架)...

My unit testing code looks as follows (I use Moq as my Mocking Framework and Unity as my DI Framework)...

Mock<PlantTreeNodeSelectedEvent> eventBeingListenedTo = new Mock<PlantTreeNodeSelectedEvent>();
eventAggregatorMock.Setup(e => e.GetEvent<PlantTreeNodeSelectedEvent>()).Returns(eventBeingListenedTo.Object);


//Initialize the controller to be tested.
IPlantTreeController controllerToTest = container.Resolve<IPlantTreeController>();


//Verify.
eventBeingListenedTo.Verify(
    e => e.Subscribe(It.IsAny<Action<string>>(), ThreadOption.UIThread));

正在调用此订阅方法(已通过调试器运行进行了验证),但验证始终失败,并显示未在模拟上执行调用:e => e.Subscribe ..."

This subscribe method IS being called (I've verified by running with the debugger), but the Verify always fails with "Invocation was not performed on the mock: e => e.Subscribe..."

知道我在做什么错吗?

推荐答案

使用这样的模拟聚合器(对于Rhino.Mocks) http://adammills.wordpress.com/2010/12/13/auto-mocking-eventaggregator/

use a mocking aggregator like this (for Rhino.Mocks) http://adammills.wordpress.com/2010/12/13/auto-mocking-eventaggregator/

如果使用ThreadOption.UIThread,它将调用Dispatcher.Invoke,如果没有消息循环,它将无法工作;通常不会在单元测试中运行.

If you use ThreadOption.UIThread, it calls Dispatcher.Invoke which won't work without a Message Loop; which isn't normally running in unit tests.

这篇关于为什么我的Moq IEventAggregator验证失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆