【发布时间】:2017-05-10 04:13:09
【问题描述】:
我正在使用 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 ?
非常感谢您阅读所有内容并提供答案!!!
朱利安
【问题讨论】:
-
为什么需要这么复杂?出于某种原因,您是否必须测试 实际 导航服务?否则,只需将模拟传递给您的视图模型,执行命令并检查模拟导航服务上的正确方法是否已使用正确的参数调用。
-
我在这里要测试的是视图模型,而不是导航。为了创建视图模型,我使用了导航模拟。然后,在调用视图模型方法后,我检查当前页面是否已正确更改。为此,我查看了导航模拟。
-
@Haukinger,您的建议正是我所做的。我的问题是我不知道如何使用 Prism 源代码提供的模拟来实现这一点。
-
我刚刚更新了我的问题。我重新组织了测试类的代码,所以测试方法在顶部。这样一来,更清楚的是使用模拟的导航,而不是视图模型。
-
使用最小起订量,您只需创建一个
new Mock<INavigationService>,将其传递给您的视图模型,然后调用Verify( x => x.NavigateAsync(...whatever parameters you expect...), Times.Once)我真的不明白您为什么要从使用的棱镜复制一个模拟如果要测试使用导航的代码,请测试导航..
标签: unit-testing navigation prism