1

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 ?
   }
}
empo
  • 1,133
  • 5
  • 21
  • 41

1 Answers1

3

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.

AxelEckenberger
  • 16,628
  • 3
  • 48
  • 70