kentcb / PCLMock

A simple mocking framework in a PCL.
MIT License
45 stars 8 forks source link

Using Verify with Prism's NavigationService and NavigationParameter #34

Closed karlingen closed 6 years ago

karlingen commented 6 years ago

I'm trying to verify that a navigation method on the NavigationService is called exactly once with the following code:

var navigationService = new NavigationServiceMock();
var viewModel = new CatalogPageViewModel(navigationService);

// Call
viewModel.CreateNewItemCommand.Execute(null);
var item = GetLatestCreatedItem();
var parameters = new NavigationParameters();
parameters.Add("id", item.Id);

// Verify
navigationService.Verify(x => x.NavigateAsync("EditPage", parameters)).WasCalledExactlyOnce();

However it yields the following exception:

PCLMock.VerificationException : Verification that NavigateAsync(It.Is("EditPage"), It.Is(?=id=89003df7-36a0-4319-8299-1195f3b7b5de [Prism.Navigation.NavigationParameters])) was called exactly once failed because it was called 0 times.
 at 
PCLMock.VerifyContinuation.ThrowVerificationException (System.String format, System.Object[] args) [0x00011] in
....

I've added breakpoints and the NavigateAsync sure is being hit with the exact same parameters, so I assume it fails because it expects the parameters to be an exact object and not another instance of the same class.

I would like to verify that a NavigationParameters is passed to the NavigateAsync and that the NavigationParameters should have a id key with the value of the Item.Id.

Is this possible?

kentcb commented 6 years ago

It.Is (which is what is automatically used if you just pass in arguments directly, like "EditPage" and parameters in your code) uses object.Equals so overriding Equals in your NavigationParameters may be one option if it's under your control. You could also use It.Matches instead.

karlingen commented 6 years ago

I love it, thanks! ❤️

var parameters = new NavigationParameters();
parameters.Add("id", item.Id);

navigationService
  .Verify(x => x.NavigateAsync("EditPage", It.Matches<NavigationParameters>(y => y.ToString() == parameters.ToString())))
  .WasCalledExactlyOnce();