tags:

views:

77

answers:

1

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");
+3  A: 

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

Chris Koenig
Yes, thanks Chris, I used the 2nd option and this seems pretty good to me. Perhaps instead of a string, for the token, I'll create a MessengerSimpleTokens enum.
Jeff Zickgraf