单元测试-单元测试棱镜导航

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

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

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

例如,查看此方法(来自PageNavigationServiceFixture类):

    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]);
    }

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

现在这是我的测试课:

[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个问题(代码中已注明)

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

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 / ContenuPage / ClubHousePage?

非常感谢您阅读全部内容以及您的回答!
朱利安

最佳答案
您建议的是对Prism进行单元测试,而不是您的代码.对ViewModel进行单元测试是一件好事,但是您只需要提供一个Mock,即可为您提供一种方法来验证您期望的导航字符串/参数是否有效.例如,您的Mock可能类似于:

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来验证应用程序的导航和用户体验.

转载注明原文:单元测试-单元测试棱镜导航 - 代码日志