执行单元测试后,收到错误消息“预期对模拟至少调用一次,但从未执行”

时间:2019-02-19 13:22:39

标签: unit-testing xamarin.forms async-await xunit

我有以下代码,该代码使用Xamarin表单中的Navigation Service从一页导航到另一页。 在单击观察按钮时,它将执行观察命令,如下所示。

public Command ObservationsCommand => new Command(async () => await OnObservationsCommandAsync());

点击观察按钮后,它将导航到下一页,将所选数据传递到导航服务,如图所示

private async Task OnObservationsCommandAsync()
            {
                ObservationDetailsParameter selectedData = new ObservationDetailsParameter
                {
                    Cage = DisplayedCage,
                    Dossiers = DossierList.SelectedItems
                };
                await _navigationService.NavigateToAsync<ObservationDetailsViewModel>(selectedData);
            }

下面是单元测试的代码

 [Fact]
    public void TestOnObservationsCommandAsync()
    {
        var mockNavigationService = new Mock<INavigationService>();
        var mockCageDetailsService = new MockCageDetailsService();
        var mockObservationDetailsService = new MockObservationDetailsService();
        var mockSettingsService = new MockSettingsService();
        Cage _displayedCage = new Cage { Id = 11 };
        Dossier _dossier1 = new Dossier { Id = 841 };
        var _dossierList = new SelectableItemCollection<Dossier>
        {
            _dossier1
        };
        _dossierList.SelectAll();
        var cageObsViewModel = new CageObsViewModel(mockNavigationService.Object, mockCageDetailsService, mockSettingsService);
        var mockObservationDetailsParameter = new 
Mock<IObservationDetailsParameter>();

        mockObservationDetailsParameter.Setup(x => 
x.Cage).Returns(_displayedCage);
        mockObservationDetailsParameter.Setup(x => x.Dossiers 
).Returns(_dossierList.SelectedItems );

        cageObsViewModel.DisplayedCage = mockObservationDetailsParameter .Object .Cage ;
        cageObsViewModel.DossierList = _dossierList;

        // Act
        cageObsViewModel.ObservationsCommand.Execute(null);
        mockNavigationService.Verify((s) => s.NavigateToAsync<ObservationDetailsViewModel>(mockObservationDetailsParameter.Object));
    }

但是,执行此测试后,我收到错误消息“对模拟进行了至少一次预期调用,但从未执行。” 你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

这是单元测试的示例,我必须在成功登录后验证导航。

[Fact]
        public async Task ValidCredentials()
        {
            //Mock login -> false
            var authenticationServiceMock = new Mock<IAuthenticationService>();
            authenticationServiceMock
                .Setup(s => s.Login(It.IsAny<string>(), It.IsAny<string>()))
                .Returns(Task.FromResult(true));

            LoginViewModel viewModel = new LoginViewModel(authenticationServiceMock.Object);

            await viewModel.InitializeAsync(null);

            viewModel.UserName.Value = "johann@mail.com";
            viewModel.Password.Value = "1234!Maaaa";

            viewModel.LoginCommand.Execute(null);

            navigationServiceMock.Verify((s) => s.NavigateToAsync<HomeViewModel>());
        }

当我使用AutoFac时,我的ViewModelBase本身获取了NavigationService实例,这是我的测试设置:

public LoginViewModelTests()
        {
            ContainerBuilder builder = new ContainerBuilder();

            navigationServiceMock = new Mock<INavigationService>();

            navigationServiceMock.SetReturnsDefault<Task>(Task.FromResult(0));

            builder.RegisterInstance<INavigationService>(navigationServiceMock.Object);

            ViewModelLocator.RegisterDependencies(builder);
        }

如果不清楚,请告知我,我们可以对其进行审核,以更好地适应您的情况。