无法将 Castle.Proxies.INavigationServiceProxy 类型的对象转换为 Prism.Common.IPageaware

Unable to cast object of type Castle.Proxies.INavigationServiceProxy to Prism.Common.IPageaware

我一直在使用 INavigationService 的模拟来执行一些单元测试,但当应用程序尝试验证当前 URI 路径时出现错误。验证这一点的代码行如下:

if (_navigationService.GetNavigationUriPath().Contains("Register"))
                RegisterFlag = true;

下面是 INavigationService 的模拟:

Mock<INavigationService> _navServ;
...
        [SetUp]
        public void Setup()
        {
            Properties.Api = new MockService();
            _navServ = new Mock<INavigationService>();
        }

当我调试我的任何测试时,我在我提供的代码的第一行收到错误“无法将 Castle.Proxies.INavigationServiceProxy 类型的对象转换为 Prism.Common.IPageaware”。 我想了解为什么会发生这种情况以及我可以做些什么来解决这个问题。

如果你看 code of GetNavigationUriPath (which you should really have done, as the stack trace of the exceptions points to it), you see that you have to setup the Page-property of your navigation-service mock (see the docs of moq):

_navServ = new Mock<INavigationService>();
_navServ.As<IPageAware>().Setup( x => x.Page ).Returns( /* your current page */ );