I have a MvxViewModel with three commands, two of which change the view model and the third provides navigation. I have a method of testing the navigation as discussed in this blog post but I am struggling to find a method of testing the GetClubsCommand and ClubItemSelectionCommand commands
ViewModel snippet
public ICommand GetClubsCommand
{
get { return new MvxCommand(() => Task.Run(async () => await GetClubs())); }
}
public ICommand ConfirmClubSelectionCommand
{
get { return new MvxCommand(() => ShowViewModel<LogonViewModel>(SelectedClub)); }
}
public ICommand ClubItemSelectionCommand
{
get { return new MvxCommand<ClubDto>(c => SetSelectedItem(c)); }
}
Tests
[Test]
public void GetClubsCommand()
{
var dataServiceMock = Mock.Create<IClubDataService>();
Mock.Arrange(() => dataServiceMock.GetClubs(Arg.AnyString))
.Returns(() => GetClubs());
ClubSelectionViewModel vm = new ClubSelectionViewModel(dataServiceMock);
//Act
vm.GetClubsCommand.Execute(null);
//Assert
Assert.IsNotNull(vm.Clubs);
}
[Test]
public void SetClubItemSelectionCommand()
{
var dataServiceMock = Mock.Create<IClubDataService>();
Mock.Arrange(() => dataServiceMock.GetClubs(Arg.AnyString))
.Returns(() => GetClubs());
ClubSelectionViewModel vm = new ClubSelectionViewModel(dataServiceMock);
//Act
vm.ClubItemSelectionCommand.Execute(null);
//Assert
Assert.IsNotNull(vm.SelectedClub);
}
When I run the above tests they fail on the construction of the MvxCommand.
System.NullReferenceException : Object reference not set to an instance of an object.
at xxx.Members.Core.ViewModels.ClubSelectionViewModel.SetSelectedItem(ClubDto club) in Y:\Documents\Development\Projects\xxx\xxx.Members\ViewModels\ClubSelectionViewModel.cs:line 69
at xxx.Members.Core.ViewModels.ClubSelectionViewModel.<get_ClubItemSelectionCommand>b__23_0(ClubDto c) in Y:\Documents\Development\Projects\xxx\xxx.Members\ViewModels\ClubSelectionViewModel.cs:line 63
at Cirrious.MvvmCross.ViewModels.MvxCommand`1.Execute(Object parameter)
at xxx.Members.Core.Tests.ClubSelectionViewModelTests.SetClubItemSelectionCommand() in Y:\Documents\Development\Projects\xxx\UnitTests\xxx.Members.Core.Tests\ClubSelectionViewModelTests.cs:line 148
IS there a recommended method of unit testing the MvxCommand in this case?
Aucun commentaire:
Enregistrer un commentaire