MVVM Light Messenger - 如何对目标消息进行单元测试?

发布于 2024-09-08 21:00:21 字数 178 浏览 1 评论 0原文

如果我将消息从 ViewModelA 发送到 ViewModelB,有没有办法从正在测试引发消息的 ViewModelA 的单元测试中捕获此通知?

Messenger.Default.Send<string, ViewModelB>("Something Happened");

If I target a message from ViewModelA to ViewModelB, is there a way to catch this notification from my unit test that is testing ViewModelA where the Message is raised?

Messenger.Default.Send<string, ViewModelB>("Something Happened");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

醉梦枕江山 2024-09-15 21:00:21

我看到两个选项:

首先,您可以使用“标记”接口标记 ViewModelB,并使用它代替实际的类名。

Messenger.Default.Send<string, IMessageTarget>("Something Happened"); 

这不是我最喜欢的解决方案,但它应该有效。

或者,您可以在 ViewModelB 中使用特定令牌注册消息,同时从 ViewModelA 发送消除歧义的消息:

In ViewModelA:

Messenger.Default.Send<string>("Something Happened", "MessageDisambiguator");

In ViewModelB:

Messenger.Default.Register<string>(
    this, 
    "MessageDisambiguator", 
    (action) => DoWork(action)
);

更加清晰,并且仍然允许您模拟 ViewModelB 以用于测试目的。

可能还有更多的选择,但这些是在这么晚的时候突然出现在我脑海中的选项......

I see two options:

First, you could mark ViewModelB with a "marker" interface, and use that instead of your actual class name.

Messenger.Default.Send<string, IMessageTarget>("Something Happened"); 

This is not my favorite solution, but it should work.

Or, you could register for messages with a specific token in ViewModelB while sending the disambiguated message from ViewModelA:

In ViewModelA:

Messenger.Default.Send<string>("Something Happened", "MessageDisambiguator");

In ViewModelB:

Messenger.Default.Register<string>(
    this, 
    "MessageDisambiguator", 
    (action) => DoWork(action)
);

Much cleaner, and will still allow you to mock out ViewModelB for testing purposes.

There could be more options, but these are the ones that pop to the top of my head at this late hour...

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