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

If I am targeting a message from ViewModelA to ViewModelB, is there a way to catch this notification from my unit test, which tests ViewModelA, where the message appears?

Messenger.Default.Send<string, ViewModelB>("Something Happened");
+3
source share
1 answer

I see two options:

First, you can mark ViewModelB with a marker and use this 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 can register for messages with a specific token in ViewModelB when sending an ambiguous message from ViewModelA:

In ViewModelA:

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

In ViewModelB:

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

Significantly cleaner and still allows you to mock ViewModelB for testing purposes.

, , ...

+6

Source: https://habr.com/ru/post/1754152/


All Articles