I have a new Xamarin Forms (3.2) project using .NetStandard library (2.0.3) and Prism/Unity for navigation and dependency injection. I have been struggling to set up basic unit tests.
My unit test project has a single unit test class that is trying to test a method in a ViewModel class (LoginPageViewModel). The ViewModel class inherits from a ViewModelBase which inherits from Prism's BindableBase and implements Prism's INavigationAware interface.
public class ViewModelBase : BindableBase, INavigationAware, IDestructible
I have two constructors. The second I created just for the unit test because I don't need the INavigationService for the tests.
public LoginPageViewModel(INavigationService navigationService, IdentityService identityService) : base(navigationService)
public LoginPageViewModel(IIdentityService identityService) : base()
The project builds, but I get this exception for each of my tests:
Message: Test method SalesOrderApp.UnitTests.LoginPageViewModelTests.UserLogin_LoginFails_ReturnsFalseAndNoToken threw exception: System.TypeLoadException: Could not load type 'Prism.Navigation.INavigationAware' from assembly 'Prism, Version=7.1.0.431, Culture=neutral, PublicKeyToken=40ee6c3a2184dc59'.
I have the same version of Prism.Unity.Forms (7.1.0.431) installed in the MSTest project as I have in the project that I am testing. I have been struggling to get the MSTests to work with Prism. Any help would be very much appreciated.
Without a reproduction I can only make assumptions here. Based on the exception it would appear that it cannot resolve the Navigation interfaces. This tells me that your unit test project is targeting the full .Net Framework (net45) which the only scenario that would cause that exception.
You can retarget your unit tests to either a device runner which I recommend if you’re using Xamarin, or simply netcore.