Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Unit testing ViewModels

$
0
0

I appreciate this is a bit of a general question, but I thought someone might be able to direct me.

I've written a sample application to show my students an example of MVVM . One of the motivations for MVVM is loose coupling and hence easier unit testing.

For my ViewModel, everything is working out so far except for one small detail - Commands.

In my view model constructor, I have the following lines which are dependent on Xamarin.Forms:

  1. In the constructor of the ViewModel

    FetchNextSayingCommand = new Command(execute: async () => await DoFetchNextMessageCommand(), canExecute: () => NetworkIsIdle);

where DoFetchNextMessageCommand() simply asks the model to fetch a new string from Azure and NetworkIsIdle is a bool property that is false during a network transaction.

  1. When listening for an event from the Model to say the network status has changed:

    (Command)FetchNextSayingCommand).ChangeCanExecute()

I think my question reduces down to two fundamentals:

  1. Should a ViewModel aspire to have no (tightly coupled) dependencies on Forms or am I simply being too purist?
    (i.e. such that I can comment out using Xamarin.Forms; from the head of my source)
  2. Is there a considered best-practice for unit testing Commands?
    (I have my own ideas, but I suspect someone else has already solved this)

Many thanks,

Nick


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>