gpt4 book ai didi

c# - 如何为 Prism 中的单元测试导航设置 ServiceLocationProvider?

转载 作者:行者123 更新时间:2023-11-30 21:55:53 26 4
gpt4 key购买 nike

我正在处理的一个 Prism/Unity 应用程序有一个 View ,负责实现 RegionNavigationJournal 的 GoBack()GoForward() 方法。这将是我第一次需要对使用 RegionManager 的任何东西进行单元测试,我遇到了一个错误,提示

ServiceLocationProvider must be set

当我尝试定位其导航被控制的区域时(该区域与相关 View 所属的区域不同)。

基于此,我进行了一些挖掘,但没有发现任何有关解决 Prism 导航上下文中的“ServiceLocationProvider”错误的信息。如果可能的话,我宁愿避免 mock 任何东西,但如果那最终成为唯一/最好的选择,那也不是不可能的。

如果我可以包含更多信息,请告诉我。感谢您提供的任何见解!

最佳答案

阅读更多内容后发现 ServiceLocationProvider 是作为 Bootstrapper.Run() 的一部分启动的。在创建虚拟 Bootstrap 时遇到了一些问题,因为我不确定如何实现 CreateShell,但我意识到您可以返回一个新的 DependencyObject

这是我的测试 Bootstrap 的样子:

 public class TestBootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return new DependencyObject();
}
}

在测试开始时需要完成以下操作以添加区域:

TestBootstrapper bootstrapper = new TestBootstrapper();
bootstrapper.Run();

this.RegionManager = new RegionManager();
this.RegionManager.Regions.Add(UIRegionNames.ContextResultsPane, new Region());

在正在测试的 ViewModel 的构造函数中,我执行以下操作以获取导航服务日志的实例:

public NavigationContextControlViewModel(IRegionManager regionManager)
{
IRegionNavigationService navigationService;

this.RegionManager = regionManager;
navigationService = this.RegionManager.Regions[UIRegionNames.ContextResultsPane].NavigationService;
navigationService.Navigated += CheckIfNavigateCanExecuteHasChanged;
this.ContextResultsNavigationJournal = navigationService.Journal;
}

关于c# - 如何为 Prism 中的单元测试导航设置 ServiceLocationProvider?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31838797/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com