MVVM Light Messenger - 如何对目标消息进行单元测试?
如果我将消息从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到两个选项:
首先,您可以使用“标记”接口标记 ViewModelB,并使用它代替实际的类名。
这不是我最喜欢的解决方案,但它应该有效。
或者,您可以在 ViewModelB 中使用特定令牌注册消息,同时从 ViewModelA 发送消除歧义的消息:
In ViewModelA:
In ViewModelB:
更加清晰,并且仍然允许您模拟 ViewModelB 以用于测试目的。
可能还有更多的选择,但这些是在这么晚的时候突然出现在我脑海中的选项......
I see two options:
First, you could mark ViewModelB with a "marker" interface, and use that instead of your actual class name.
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:
In ViewModelB:
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...