如何在UIThread上测试Prism事件聚合器订阅? [英] How do I test Prism event aggregator subscriptions, on the UIThread?

查看:329
本文介绍了如何在UIThread上测试Prism事件聚合器订阅?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,该类通过PRISM事件聚合器订阅事件.

I have a class, that subscribes to an event via PRISMs event aggregator.

由于要模仿此处那样的事件聚合器有些困难,因此

As it is somewhat hard to mock the event aggregator as noted here, I just instantiate a real one and pass it to the system under test.

然后在测试中,我通过该聚合器发布事件,然后检查被测系统对该事件的反应.由于该事件将在生产期间由FileSystemWatcher引发,因此我想通过订阅UIThread来利用自动分派,因此一旦引发该事件,我就可以更新我的UI.

In my test I then publish the event via that aggregator and then check how my system under test reacts to it. Since the event will be raised by a FileSystemWatcher during production, I want to make use of the automatic dispatch by subscribing on the UIThread, so I can update my UI once the event is raised.

问题是,在测试期间,除非我不订阅UIThread,否则该事件不会在被测系统中引起注意.

The problem is, that during the test, the event never gets noticed in the system under test unless I don't subscribe on the UIThread.

我正在使用MSpec进行测试,我是通过TDD.Net在VS2008内部运行的. 在我的测试课程中添加[RequiresSta]并没有帮助

I am using MSpec for my tests, which I run from inside VS2008 via TDD.Net. Adding [RequiresSta] to my test class didn't help

有没有人有解决方案,可以避免我在测试期间更改ThreadOption(例如,通过属性-什么是丑陋的黑客手段)?

Does anyone have a solution, that saves me from changing the ThreadOption during my tests (e.g. via a property - what an ugly hack)???

推荐答案

如果同时模拟事件和事件聚合器,并使用moq的Callback,则可以做到.

If you mock both the event and the Event Aggregator, and use moq's Callback, you can do it.

这是一个例子:

Mock<IEventAggregator> mockEventAggregator;
Mock<MyEvent> mockEvent;

mockEventAggregator.Setup(e => e.GetEvent<MyEvent>()).Returns(mockEvent.Object);

// Get a copy of the callback so we can "Publish" the data
Action<MyEventArgs> callback = null;

mockEvent.Setup(
    p =>
    p.Subscribe(
        It.IsAny<Action<MyEventArgs>>(), 
        It.IsAny<ThreadOption>(), 
        It.IsAny<bool>(), 
        It.IsAny<Predicate<MyEventArgs>>()))
        .Callback<Action<MyEventArgs>, ThreadOption, bool, Predicate<MyEventArgs>>(
        (e, t, b, a) => callback = e);


// Do what you need to do to get it to subscribe

// Callback should now contain the callback to your event handler
// Which will allow you to invoke the callback on the test's thread
// instead of the UI thread
callback.Invoke(new MyEventArgs(someObject));

// Assert

这篇关于如何在UIThread上测试Prism事件聚合器订阅?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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