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

Total
1
Shares

I have been using a mock of INavigationService in order to conduct some unit tests and I get an error when the application tries to verify the current URI path. The line of code that verifies this is the following:

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

And here is the mock of the INavigationService:

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

When i debug any of my tests i get the error "Unable to cast object of type Castle.Proxies.INavigationServiceProxy to Prism.Common.IPageaware" on the first line of code i present.
I would like to understand why this happens and what I can do to fix this issue.


Solution

If you look at the 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 */ );
Leave a Reply

Your email address will not be published. Required fields are marked *