empo
empo

Reputation: 1163

Unit test MVVM Light Messenger message is sent when a RelayCommand is executed

How do I unit test that the messenger send was called when a RelayCommand is executed?

ViewModelClass:

public class MyViewModel
{
   public MyViewModel()
   {
      this.MyCommand = new RelayCommand(() => SendMyMessage());
   }

   public int Id { get; set; }
   public RelayCommand MyCommand { get; private set; }

   private void SendMyMessage()
   {
      Messenger.Default.Send<int, OtherViewModel>(this.Id);
   }
}

Unit Test:

[TestClass]
public class When_MyCommand_Is_Executed
{
   [TestMethod]
   public void A_Message_Is_Sent()
   {
      //Arrange
      var vm = new MyViewModel();

      //Act
      vm.MyCommand.Execute(1);

      //Assert
      //What to do here ?
   }
}

Upvotes: 1

Views: 1059

Answers (1)

AxelEckenberger
AxelEckenberger

Reputation: 16926

Register on the Messenger as a recipient of the message, store the message in a variable and then check that it is the proper message you received.

Upvotes: 3

Related Questions