Replies: 1 comment 1 reply
-
Due to requiring this functionality now I am building apps with Maui, I have rolled my own library which does exactly what this discussion needs. Moq.INavigationService, I have published it on nuget but if you are running newer builds of prism than the public prism maui (such as sponsor connect), you will need to either build this code from source referencing your newer prism version or pull in the source code locally. This will be an ongoing issue until there is a public release of prism 9 for maui. I am open to feedback on this lib and happy to donate it over to prism if thats what you guys want |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In Xamarin Forms the
INavigationService
was incredible mock friendly, having all of it's api's available on its interface:This made writing unit tests very easy using a library such as Moq since you could run unit tests on a viewmodel to test navigation without having to run the UI or mock the virtual UI using MockForms:
When porting a Xamarin Forms app to Maui, or starting a new Maui project this old way of mocking the navigation service is no longer possible. The navigation service interface has less methods and only a single way of mocking
NavigateAsync
, these api's have been retained but as Extension methods as opposed to interface methods. This has allowed for the new navigation builder, which I have personally really enjoyed using and I think is a fantastic addition to Prism.This new design has caused one issue when it comes to testing. With most navigation methods being Extension methods, they are hard to mock and porting tests from forms to maui might be incredibly difficult for teams who have done alot of mocking of the navigation service.
Using a
Mock<INavigationService>
without any setup can cause unexpected errors when running tests. For example when callingNavigateAsync("somestring")
, without any setup the following exception will be thrown:It is very possible to write a custom mock implementation of the navigation service to handle mocking calls like we did with Prism.Forms. In a POC app I did just that by creating a custom
INavigationService
which usedMock<INavigationService>
to perform setup & verifications but was compatible with the extension methods.Prism.Maui.Testing could be a valueable addition to the prism ecosystem.
It might be very beneficial for everyone currently using Prism in Xamarin Forms and those considering it for Maui to provide a convenient way of setting up & mocking the navigation service for the purposes of unit testing.
I am currently writing my own mini library to do just this, its very much designed for my needs and probably not appropriate for the prism title (hence why i didn't give it one) but the concept would certainly help anyone using Prism.Maui and writing unit tests!
Prism.Maui.Testing
could be a valuable addition to the prism ecosystem!Beta Was this translation helpful? Give feedback.
All reactions