使用 Moq 进行单元测试的模拟 Prism 事件聚合器
我需要一些关于如何在单元测试中使用 Moq 的建议,以确保我的测试类按照我想要的方式运行。也就是说,被测试的类发布了一个事件聚合器(来自 Prism)事件,我需要某种方式来断言该事件已在我的测试中引发。
我在工作中没有太多资源,并且发现很难知道如何进行设置。
我:-
public SomeEvent : CompositePresentationEvent<SomeEvent>
{
EventPayload
}
public SomeClass
{
void Allocate(){EventAggregator.Publish<SomeEvent>}
}
public SomeService : IService
{
SomeService(){ EventAggregator.Subscribe<SomeEvent>(DoSomething)}
void DoSomething(SomeEvent evt){}
}
我认为,如果我的测试是针对 SomeClass 的,我需要验证是否调用 SomeClass.Allocate 是否正在发布 SomeEvent 消息。这是怎么做到的?
我是否还需要验证模拟的 SomeService 是否正在接收 SomeEvent?或者这是属于 SomeService 单元测试而不是 SomeClass 的单独单元测试?
无论如何,不知道如何设置这些,所以任何建议将不胜感激。
I need some advice on how to use Moq in a unit test to make sure that my class under test is behaving how I want. That is the class under test publishes an Event Aggregator (from Prism) event and I need some way of asserting that this event has been raised in my test.
I don't have a lot of resource at work and am finding it difficult to know how to set this up.
I have :-
public SomeEvent : CompositePresentationEvent<SomeEvent>
{
EventPayload
}
public SomeClass
{
void Allocate(){EventAggregator.Publish<SomeEvent>}
}
public SomeService : IService
{
SomeService(){ EventAggregator.Subscribe<SomeEvent>(DoSomething)}
void DoSomething(SomeEvent evt){}
}
I think that if my test is for SomeClass I need to verify that if I call SomeClass.Allocate a SomeEvent message is being published. How is this done?
Do I also need to verify that a mocked SomeService is receiving the SomeEvent? Or is that a seperate unit test that belongs to SomeService unit test and not SomeClass?
In any event, not sure how to set any of this up so any advice would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以为
SomeClass
提供一个IEventAggregator
,这将允许您在测试期间提供模拟:然后您的测试将如下所示:
如果这些是单元测试,那么您将在
SomeService
测试中完全单独测试订阅。您正在测试SomeClass
是否正确发布事件,以及SomeService
在收到要处理的事件时是否正确运行。You would supply
SomeClass
with anIEventAggregator
, which will allow you to supply a mock during testing:Then your test would look something like this:
If these are unit tests then you would test the subscription entirely separately in the
SomeService
tests. You are testing thatSomeClass
correctly publishes an event and thatSomeService
behaves correctly when it is given an event to process.