wpfunit-testingmvvmcatel

Testing this command


I'm quite new to unit tests and I've got some troubles testing this command

internal async Task OnDeleteTreasurerCommandExecute(TesorieraItemResult tesoriera)
    {
        try
        {
            if (await MessageService.ShowAsync("Confermare l'operazione?", string.Empty, MessageButton.YesNo, MessageImage.Question) == MessageResult.Yes)
            {
                await repository.DeleteTesorieraItemAsync(tesoriera.ID_ISTITUTO,tesoriera.ID_DIVISA,tesoriera.PROGRESSIVO);

                await MessageService.ShowInformationAsync("Operazione completata");

                if (SelectedInstitute != null)
                    await OnLoadDataCommandExecute();
            }
        }
        catch (Exception ex)
        {
            ErrorService.HandleError(GetType(), ex);
        }
    }

I'm using Catel as MVVM framework

how do I simulate the yes/no answers? Thanks


Solution

  • You need to substitute the MessageService with a class that can return yes or no answer. Here's an example using NSubstitute.

    1. Install-Package NSubstitute

    2. Install-Package NUnit

    3. Let us say you have a class that has a method that needs Yes, then No:

      public class AccountViewModel
      {
          readonly IMessageService _messageService;
          readonly ICustomerRepository _customerRepository;
      
          public AccountViewModel(IMessageService messageService, ICustomerRepository customerRepository)
          {
              _messageService = messageService;
              _customerRepository = customerRepository;
          }
      
          public async Task OnDeleteCustomer(Customer customer)
          {
              if (await MessageService.ShowAsync(
                 "Confirm?", 
                  string.Empty, 
                  MessageButton.YesNo, 
                  MessageImage.Question) == MessageResult.Yes)
              {
                  _customerRepository.Delete(customer);
                  await MessageService.ShowInformationAsync("Completed");
              }
          }
      }
      

    Then your test case looks like this:

    public class TestAccountViewModel
    {
        [TestCase]
        public class TestDeleteCustomer()
        {
            // arrange
            var messageService = Substitute.For<IMessageService>();
            messageService
                .ShowAsync(
                    Arg.Any<string>(),
                    Arg.Any<string>(),
                    Arg.Any<MessageButton>(),
                    Arg.Any<MessageImage>())
                .Returns(Task.FromResult(MessageResult.Yes);
    
            messageService
                .ShowInformationAsync(Arg.Any<string>())
                .Returns(Task.FromResult<object>(null));
    
            var customerRepository = Substitute.For<ICustomerRepository>();
    
            // act
            var sut = new AccountViewModel(messageService, customerRepository);
            var customer = new Customer();
            sut.OnDeleteCustomer(customer);
    
            // assert
            Assert.IsTrue(customerRepository.Received().DeleteCustomer(customer));
        }
    }