开发者

MVVM Light Messenger - How do you unit test targeted messages?

If I target a message from ViewModelA to ViewModelB, 开发者_JAVA百科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");


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜