Xamarin.Forms BDD Unit Testing in Xamarin.Forms Advanced Usage for MVVM


To add to the first example, in order to test navigation statements that occurs within the application, we need to provide the ViewModel with a hook to the Navigation. To achieve this:

  • Add the package SpecFlow.Xamarin.Forms.IViewModel from nuget to your PCL Xamarin.Forms project
  • Implement the IViewModel interface in your ViewModel. This will simply expose the Xamarin.Forms INavigation property:
  • public class MainViewModel : INotifyPropertyChanged, IViewModel.IViewModel { public INavigation Navigation { get; set; }
  • The test framework will pick that up and manage internal navigation
  • You can use any MVVM frameworks for you application (such as XLabs, MVVMCross, Prism to name a few. As long as the IViewModel interface is implemented in your ViewModel, the framework will pick it up.