【问题标题】:Mock and test MvvmCross NavigationService模拟和测试 MvvmCross NavigationService
【发布时间】:2017-12-18 13:30:59
【问题描述】:

我正在使用导航服务在 ViewModel 之间执行导航,现在我必须为我的 ViewModel 创建一些单元测试。我可以使用 Moq 模拟创建注入对象的 ViewModel,但我正在努力模拟 NavigationService,然后创建断言,而不是导航到下一个 ViewModel。

我找到了几年前的一些文档,所以导航服务不存在,我在 MvvmCross 官方文档中找不到任何关于这个问题的信息,所以现在我陷入了死胡同。

我正在使用 MvvmCross 5.5.0。

[Test]
public void CheckStatus()
{
    Mock<IStatusService> _mockStatusService = new Mock<IStatusService>();
    _mockStatusService.Setup(x => x.Check(It.IsAny<StatusRequest>())).Returns(() => new StatusRequest { Ok = true });

    //Here I need to pass the NavigationService to the constructor
    var _viewModel = new StatusViewModel(_mockStatusService.Object, navigationService);
    _viewModel.Start();

    //Here I guess I should perform the navigation assertion.
}     

【问题讨论】:

    标签: xamarin mvvmcross


    【解决方案1】:

    我找到了如何模拟和测试 NavigationService。我在这里发布解决方案,因为它对其他人有用。

    [Test]
    public void CheckStatus()
    {
        IFixture _fixture = new Fixture().Customize(new AutoMoqCustomization());
        Mock<IMvxNavigationService> _navigationService = _fixture.Freeze<Mock<IMvxNavigationService>>();
    
        var _viewModel = new StatusViewModel(navigationService);
        _viewModel.Start();
    
        _navigationService.Verify(x => x.Navigate<NextViewModel>(null), Times.Once());
    }     
    

    【讨论】:

    • 可以使用ViewModelLoader不手动调用start等方法。
    猜你喜欢
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    相关资源
    最近更新 更多