I’m trying out MVVM Light, partly inspired by the EventToCommand capabilities which seem to make it easier to handle drag-and-drop from outside my app in the View Model and in the XAML. However I am a confused by how to unit test the RelayCommand. My RelayCommand is declared simply
public RelayCommand<DragEventArgs> DropFile { get; private set; }
and then the functionality is assigned within the ViewModel constructor, not inline but using a method on the ViewModel
this.DropFile = new RelayCommand<DragEventArgs>(dropFileHandler);
When I’m writing a unit test for the DropFile RelayCommand I cannot see what to call? Should I be calling
testTarget.DropFile.Execute(params)
and how does one construct the params since DragEventArgs has only an empty constructor, and its key properties are just getters not setters?
This is true for standard commands as well as the MVVM-Light specific relay commands.
logic that needs to be unit testable should be implemented in the viewmodel as a method and then called from the command.
whats left in the command should be logic to extract information from the UI, i.e. converting the parameter to the appropriate type and passing it on.
This way the viewmodel as an entity is unit testable, the commands are kept very thin, everyones happy =].
N.B. If you want to be particularly strict with your unit testing, the conversion should happen in the Method of the ViewModel, but typically so long as it can handle a null parameter then your all set which is why I grow lazy.
Hope that helps