单元测试 Prism 导航 [英] Unit test Prism navigation

查看:21
本文介绍了单元测试 Prism 导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Prism 和 Xamarin Forms 创建一个应用程序.我想对我的视图模型进行单元测试.

I'm creating an application using Prism and Xamarin Forms. I want to unit-test my view models.

我有一个导航到页面的命令.我想断言在调用该命令后导航发生在正确的页面上.

I have a command that navigate to a page. I want to assert that the navigation occured to the right page after that command has been called.

我想我需要复制 Prism/Source/Xamarin/Prism.Forms.Tests/Navigation/PageNavigationServiceFixture.cs 文件.

I guess that I need to replicate the code found in the Prism/Source/Xamarin/Prism.Forms.Tests/Navigation/PageNavigationServiceFixture.cs file.

例如,看看这个方法(来自 PageNavigationServiceFixture 类):

For example, look at this method (coming from the PageNavigationServiceFixture class) :

    public async void Navigate_ToContentPage_ByName()
    {
        var navigationService = new PageNavigationServiceMock(_container, _applicationProvider, _loggerFacade);
        var rootPage = new Xamarin.Forms.ContentPage();
        ((IPageAware)navigationService).Page = rootPage;

        await navigationService.NavigateAsync("ContentPage");

        Assert.True(rootPage.Navigation.ModalStack.Count == 1);
        Assert.IsType(typeof(ContentPageMock), rootPage.Navigation.ModalStack[0]);
    }

它创建一个导航服务,导航到一个页面,然后断言该页面已更改.

It creates a navigation service, navigate to a page and then assert that the page has changed.

现在这是我的测试课:

[TestClass]
public class MenuPrincipalViewModelTests
{
    [TestMethod]
    public void AProposCommand()
    {
        INavigationService navigationService = new PageNavigationServiceMock(_container, _applicationProvider, _loggerFacade);

        // 1: Here I need to initialize my navigation service to my MasterDetailPage

        MenuPrincipalPageViewModel viewModel = new MenuPrincipalPageViewModel(navigationService);

        viewModel.AProposCommand.Execute();

        // 2: Here I want to assert that the new navigation stack is MenuPrincipalPage/ContenuPage/AProposPage
    }

    PageNavigationContainerMock _container;
    IApplicationProvider _applicationProvider;
    ILoggerFacade _loggerFacade;

    public MenuPrincipalViewModelTests()
    {
        _container = new PageNavigationContainerMock();

        _container.Register("PageMock", typeof(PageMock));

        _container.Register("ContentPage", typeof(ContentPageMock));
        _container.Register(typeof(ContentPageMockViewModel).FullName, typeof(ContentPageMock));

        _container.Register("NavigationPage", typeof(NavigationPageMock));
        _container.Register("NavigationPage-Empty", typeof(NavigationPageEmptyMock));
        _container.Register("NavigationPageWithStack", typeof(NavigationPageWithStackMock));
        _container.Register("NavigationPageWithStackNoMatch", typeof(NavigationPageWithStackNoMatchMock));

        _container.Register("MasterDetailPage", typeof(MasterDetailPageMock));
        _container.Register("MasterDetailPage-Empty", typeof(MasterDetailPageEmptyMock));

        _container.Register("TabbedPage", typeof(TabbedPageMock));
        _container.Register("CarouselPage", typeof(CarouselPageMock));

        _container.Register("MenuPrincipalPage", typeof(MenuPrincipalPage));
        _container.Register("ContenuPage", typeof(ContenuPage));
        _container.Register("ClubHousePage", typeof(ClubHousePage));
        _container.Register("AProposPage", typeof(AProposPage));

        _applicationProvider = new ApplicationProviderMock();
        _loggerFacade = new EmptyLogger();
    }
}

构造函数是从 PageNavigationServiceFixture 复制而来的.我现在需要实现 AProposCommand 测试,我有 2 个问题(在代码中注明)

The constructor was copied from PageNavigationServiceFixture. I now need to implement the AProposCommand test and I have 2 questions (noted in the code)

如何初始化导航服务模拟以便复制我的应用程序?有关信息,这是我的 App 类:

How to initialize the navigation service mock so it's replicating my application ? For information, here is my App class :

public partial class App : PrismApplication
{
    public App(IPlatformInitializer initializer = null) : base(initializer) { }

    protected override void OnInitialized()
    {
        InitializeComponent();

        NavigationService.NavigateAsync("MenuPrincipalPage/ContenuPage/ClubHousePage");
    }

    protected override void RegisterTypes()
    {
        Container.RegisterTypeForNavigation<MenuPrincipalPage>();
        Container.RegisterTypeForNavigation<ClubHousePage>();
        Container.RegisterTypeForNavigation<AProposPage>();
        Container.RegisterTypeForNavigation<ContenuPage>();
    }
}

MenuPrincipalPage 是 MasterDetailPage,而 ContenuPage 是 NavigationPage.

MenuPrincipalPage is a MasterDetailPage and ContenuPage is a NavigationPage.

我的第二个问题是:如何断言导航堆栈现在是 MenuPrincipalPage/ContenuPage/ClubHousePage ?

My second question is : how to assert that the navigation stack is now MenuPrincipalPage/ContenuPage/ClubHousePage ?

非常感谢您阅读所有内容并提供答案!!!
朱利安

Many thanks for reading it all and for your answers !!!
Julien

推荐答案

您提出的是对 Prism 进行单元测试,而不是您的代码.对您的 ViewModel 进行单元测试是一件好事,但您只需要提供一个 Mock 来为您提供一种方法来验证您期望的导航字符串/参数是否有效.例如,您的 Mock 可能看起来像:

What you are proposing is to unit test Prism not your code. Unit testing your ViewModel is a good thing, but you only need to provide a Mock that provides you a way to verify that the navigation string/parameters you expected were valid. For example your Mock may look something like:

public class MockNavigationService : INavigationService
{
    public string LastNavigationString { get; private set; }
    public NavigationParameters LastNavigationParameters { get; private set; }

    public Task NavigateAsync(string name, NavigationParameters parameters, bool? useModalNavigation = null, bool animated = true)
    {
        LastNavigationString = name;
        LastNavigationParameters = parameters;
        return Task.FromResult(0);
    }
}

在我看来,您的真正目标不是运行单元测试,而是执行 UITest 以验证应用程序的导航和用户体验.

What it sounds like to me is that what your real goal is though is not to run a unit test but to do a UITest that could validate the navigation and User experience with your application.

这篇关于单元测试 Prism 导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆