使用 Moq 进行单元测试的模拟 Prism 事件聚合器

发布于 2024-09-12 11:47:10 字数 726 浏览 10 评论 0原文

我需要一些关于如何在单元测试中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2024-09-19 11:47:10

您可以为 SomeClass 提供一个 IEventAggregator,这将允许您在测试期间提供模拟:

public SomeClass(IEventAggregator eventAggregator)
{
     _eventAggregator = eventAggregator;
}

然后您的测试将如下所示:

var fakeEventAggregator = new Mock<IEventAggregator>();
var fakeEvent = new Mock<SomeEvent>();

fakeEventAggregator.
    Setup(x => x.GetEvent<SomeEvent>()).
    Returns(fakeEvent.Object);

var test = new SomeClass(fakeEventAggregator.Object);
test.Allocate();

fakeEvent.Verify(x => x.Publish(It.IsAny<SomeEventArgs>()));

如果这些是单元测试,那么您将在 SomeService 测试中完全单独测试订阅。您正在测试 SomeClass 是否正确发布事件,以及 SomeService 在收到要处理的事件时是否正确运行。

You would supply SomeClass with an IEventAggregator, which will allow you to supply a mock during testing:

public SomeClass(IEventAggregator eventAggregator)
{
     _eventAggregator = eventAggregator;
}

Then your test would look something like this:

var fakeEventAggregator = new Mock<IEventAggregator>();
var fakeEvent = new Mock<SomeEvent>();

fakeEventAggregator.
    Setup(x => x.GetEvent<SomeEvent>()).
    Returns(fakeEvent.Object);

var test = new SomeClass(fakeEventAggregator.Object);
test.Allocate();

fakeEvent.Verify(x => x.Publish(It.IsAny<SomeEventArgs>()));

If these are unit tests then you would test the subscription entirely separately in the SomeService tests. You are testing that SomeClass correctly publishes an event and that SomeService behaves correctly when it is given an event to process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文