samedi 31 octobre 2015

Unit Testing ICommand Windows Phone 8.1

I have a question about unit testing using MvvmLight in Windows Phone 8.1.

I have the following ViewModel class:

 public class TestViewModel:MainViewModel
{

    public TestViewModel(Test.Services.INavigationService navigationService,IDialogService dialogService)
        : base(navigationService,dialogService)
    {
        this.TestCommand = new RelayCommand(NavigateToPage);
    }


    public ICommand TestCommand{get; private set;}

    private void NavigateToPage()
   {
     this.navigationService.NavigateTo(typeof(TestApp.TestPage));
   }

This code works fine. But how can I test the ICommand "TestCommand" ?

I tried to solve this problem this way in my UnitTestApp:

    [ClassInitialize]
    public static void Initialize(TestContext testContext)
    {

       testViewModel = SimpleIoc.Default.GetInstance<TestViewModel>();
    }

    [TestMethod]
    public void TestCommand_Test()
    {
        ICommand testCommand = testViewModel.TestCommand;

        bool canExecute = testCommand.CanExecute(null);

        Assert.IsTrue(canExecute);
    }

But went out to the following failure : "UnitTestApp.TestViewModel.Initialize has wrong signature. The method must be non static, public, does not return a value and should not take any parameter.... "

Perhaps the failure is not the method "TestCommand_Test" but the "Initialize" method. Can you help me, solving this problem ?

Aucun commentaire:

Enregistrer un commentaire