Marc Ziss (2) [Avatar] Offline
#1
[Test]
public void ReceivedMessage_LoadsCounters()
{
   // Arrange
   countersService.Setup(s => s.GetAllCounters())
                      .ReturnsAsync(new List<Counter>());               1
   // Act
   publishAction.Invoke(new CountersChangedMessage(this));              2
   // Assert
   countersService.Verify(s => s.GetAllCounters());                     3
}



I think there is something missing from this unit test. publishAction is not initialized anywhere so calling Invoke errors out because it cannot call Invoke on a null value. Am I missing something?
Jim Bennett (88) [Avatar] Offline
#2
It's declared and initialised in listing 8.34:

using System;
...
Action<CountersChangedMessage> publishAction;                      (1)
...
Marc Ziss (2) [Avatar] Offline
#3
The issue was I was just doing a Subscribe not a SubscribeOnMainThread in the constructor so the Mock wasn't getting called into and therefore no initialization happened. Tricky test. Thanks for your reply.
        public CountersViewModel(ICountersService service, IMvxMessenger messenger, IMvxNavigationService navigationService)
        {
            this.service = service;
            this.navigationService = navigationService;
            token = messenger.SubscribeOnMainThread<CountersChangedMessage>(async m => await LoadCounters());
            Counters = new ObservableCollection<CounterViewModel>();
            ShowAddNewCounterCommand = new MvxAsyncCommand(ShowAddNewCounter);
        }