Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mvvm-light

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");
like image 329
Jeff Zickgraf Avatar asked Dec 05 '25 07:12

Jeff Zickgraf


1 Answers

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

like image 86
Chris Koenig Avatar answered Dec 12 '25 03:12

Chris Koenig