MvvmCross 5单元测试MvxNavigationService

时间:2017-11-16 15:18:03

标签: unit-testing mvvmcross

我正在从MvvmCross 4.4.0更新到5.2.0并更改为MvxNavigationService。 Login ViewModel已更改为使用MvxNavigationService,并且已为核心和测试注册了IMvxNavigationService。 iOS应用程序运行正常。大多数单元测试都是固定的,但是我在使用单元测试验证导航是否正确完成时遇到了问题。单元测试中的跟踪也正确导航,但MockDispatcher.Requests返回null。 MockDispatcher.Requests应该包含viewModel.OnLoginAsync()之后的值.Wait();叫做。我该如何解决这个问题?

单元测试是

       [Test]
    public void OnLogin_GivenValidCredentials_ThenAppRedirectsToMainPage()
    {
        Mock<IAuthenticationService> authenticationService = GetAuthenticationService();
        SetupCredentialValidity(authenticationService, true);
        var viewModel = GetLoginViewModel(null, authenticationService);
        viewModel.OnLoginAsync().Wait();

        Assert.IsNull(viewModel.ErrorMessage);
        Assert.AreEqual(1, MockDispatcher.Requests.Count, "Expected to navigate to the admin view model, but didn't");
        Assert.AreEqual(typeof(HomeTabBarViewModel), MockDispatcher.Requests[0].ViewModelType);
    }

方法

        public async Task OnLoginAsync()
    {
        IsActivityIndicatorVisible = true;
        try
        {
            if (Username == MobileAdminUsername && Password == MobileAdminPassword)
            {
                await _mvxNavigationService.Navigate<AdminViewModel>();
                //ShowViewModel<AdminViewModel>();
                return;
            }
            var authenticateResult = await _authenticationService.Authenticate(Username, Password);
            LoginResultsVisible = !authenticateResult.Success;
            ErrorMessage = authenticateResult.Message;
            if (authenticateResult.Success)
            {
                await _credentialCacheService.SetLastSuccessfulCredentials(Username, Password);
                //ShowViewModel<HomeTabBarViewModel>();
                await _mvxNavigationService.Navigate<HomeTabBarViewModel>();
            }
        }
        finally
        {
            IsActivityIndicatorVisible = false;
        }
    }

0 个答案:

没有答案