Unit test the ICommand Property of ViewModel – MVVM Pattern

Good to have new Patterns to design your applications. MVVM is such one pattern.

I assume the reader of this post has basic understanding of what MVVM stands for.

MVVM gives the flexibility to handle the User action in ViewModel using ICommand property. However this brings another challenge for developer of unit test this.

Consider a sample ICommand:

public ICommand ClearCommand
                if (this.infClearCommand == null)
                    this.infClearCommand = new RelayCommand(
                        param =>
                            this.SelectedFileName = string.Empty;
                            this.infFileReader = null;
                        param =>
                             return true;
                 return this.infClearCommand;             

Now to unit test this piece of code:

public void Test()
    ABCViewModel target = new ABCViewModel(); 
    target.SelectedFileName = "XYZ";
    Assert.AreEqual(target.SelectedFileName, string.Empty);

ICommand.Execute will take care of the Unit Test for you.


Set Width of Control in CodeBehind in WPF

We faced a small issue where we kept a control on WPF UI. The application was a WPF desktop application.
The width of control was required to be equal to the width of the screen. If width of UI is changed at runtime, the width of control should also increase and decrease according.
ActualWidth property solves ths problem.

WPF UI Automated Unit testing

While working on a WPF application, we created the automated unit tests. Application was developed using MVVM pattern.
Using MS Test we were able to test the Model, View Model but another challenge was how to ensure the binding is correct?
We created a utility which help us test the binding, as well as check the static property of controls used on each UI.
UI and its binding are testable now and are part of Automated unit test!