Moq,Prism 6和事件聚合的单元测试 [英] Unit testing with Moq, Prism 6, and Event Aggregation
问题描述
我想通过Event Aggregation向其抛出消息以确保其正确响应(通过适当设置属性或通过发布其他消息)来对模块进行单元测试.我正在使用棱镜6. 在我的项目中,基础架构项目具有:
I want to unit test a module by throwing messages at it via Event Aggregation to make sure it responds appropriately, either by setting properties appropriately, or by publishing other messages as a result. I am using Prism 6. In my project, the infrastructure project has:
public class ImportantMessage : PubSubEvent<string>
{
}
ModuleA发布如下消息:
ModuleA publishes a message like this:
eventAggregator.GetEvent<ImportantMessage>().Publish(importantString);
ModuleB收到这样的消息:
ModuleB receives the message like this:
eventAggregator.GetEvent<ImportantMessage>().Subscribe(HandleImportantMessage);
这里是HandleImportantMessage:
Here is HandleImportantMessage:
public void HandleImportantMessage(string importantString)
{
. . .
}
ModuleB构造函数的调用方式如下:
The ModuleB constructor is called as follows:
ModuleB(IEventAggregator EventAggregator)
此构造函数由Prism框架调用.对于单元测试,我需要创建一个ModuleB实例,并传递一个IEventAggregator,可能是Moq创建的一个伪造的.我想以一种方式来执行此操作,使我发布的消息中带有重要字符串. 如果我用Google词组带有最小起订量和事件聚合的单元测试",则有 一些参考文献,但我没有看到如何使用这些方法中的任何一种将"importantString"从ModuleA传递到ModuleB. Prism 5的示例代码创建了一个伪造的事件聚合器,但没有使用Moq.我不了解它是如何工作的,也看不到如何使用它传递字符串.
This constructor is called by the Prism framework. For unit testing, I need to create an instance of ModuleB, and pass an IEventAggregator, probably a fake one created by Moq. And I want to do this in such a way that the message I publish carries importantString with it. If I Google the phrase "unit tests with moq and event aggregation," there are several references, but I didn’t see how to use any of these approaches to pass "importantString" from ModuleA To ModuleB. The sample code for Prism 5 creates a fake event aggregator, but without using Moq. I don't understand how it works, and don't see how to pass a string with it.
我的测试代码从这样的东西开始:
My test code starts off something like this:
var moqEventAggregator = new Mock(IEventAggregator);
var moqImportantMessage = new Mock<ImportantMessage>();
moqEventAggregator.Setup(x => x.GetEvent<ImportantMessage>());
我见过的一些引用适用于.Returns(eventBeingListenedTo.Object); 应用设置后,将其添加到moqEventAggregator. 我显然需要将.Setup(something)应用于moqImportantMessage以便传递ImportantString,但我还没有确切看到.
Some of the references I have seen apply something like .Returns(eventBeingListenedTo.Object); to moqEventAggregator after Setup is applied. I obviously need to apply .Setup(something) to moqImportantMessage in order to pass importantString, but I don't see exactly what yet.
我想念什么?如何传递带有伪造的已发布消息的字符串?
What am I missing? How do I pass a string with the fake published message?
推荐答案
基本上,您需要在此处模拟两件事:
Basically you need to mock 2 things here:
- 事件聚合器
- 活动本身
鉴于您已经说了要模拟的事件:
Given you have the mock of the event you need to do as you said:
moqEventAggregator.Setup(x => x.GetEvent<ImportantMessage>()).Returns(moqImportantMessage);
模拟事件本身应该是这样的:
Mocking the event itself should be like so:
Action<string> action;
moqImportantMessage.Setup(_ => _.Subscribe(It.IsAny<Action<string>>>()))
.Callback(_action =>
{
action = _action;
});
然后您可以像这样提高订阅:
And then you can raise the subscription like so:
action("some string");
这篇关于Moq,Prism 6和事件聚合的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!