Easy Property Notification Verification in MVVM

Currently I’m doing a lot of WPF, we’re profound users of the MVVM pattern. I therefore often find myself writing a lot of unit tests for viewmodels. A, or maybe the, key feature of the MVVM pattern is the property binding between the view and the viewmodel. Once assigned a value to a property, the viewmodel notifies the view, who updates the view accordingly. A viewmodel might have a significant amount of properties who can notify the view with updates, therefore I’ve implemented two small extension methods for this purpose:

  1. public static class NotifyPropertyChanged
  2. {
  3.     public static NotifyExpectation<T>
  4.         ShouldNotifyOn<T, TProperty>(this T owner,
  5.         Expression<Func<T, TProperty>> propertyPicker)
  6.         where T : INotifyPropertyChanged
  7.     {
  8.         return CreateExpectation(owner,
  9.             propertyPicker, true);
  10.     }
  11.  
  12.     public static NotifyExpectation<T>
  13.         ShouldNotNotifyOn<T, TProperty>(this T owner,
  14.         Expression<Func<T, TProperty>> propertyPicker)
  15.         where T : INotifyPropertyChanged
  16.     {
  17.         return CreateExpectation(owner,
  18.             propertyPicker, false);
  19.     }
  20.  
  21.     private static NotifyExpectation<T>
  22.         CreateExpectation<T, TProperty>(T owner,
  23.         Expression<Func<T, TProperty>> pickProperty,
  24.         bool eventExpected) where T : INotifyPropertyChanged
  25.     {
  26.         var propertyName =
  27.             ((MemberExpression)pickProperty.Body).Member.Name;
  28.         return new NotifyExpectation<T>(owner,
  29.             propertyName, eventExpected);
  30.     }
  31. }

 

These extension methods can be used in several verification test contexts. The obvious one would be:

  1. [Test]
  2. public void OnPropertyChangedIsCalledForParticularProperty()
  3. {
  4.     // Arrange
  5.     var sut = new MyClass();
  6.  
  7.     // Act
  8.     // Assert
  9.     sut.ShouldNotifyOn(model => model.Property)
  10.         .When(model => model.Property = "new string");
  11. }

 

In this test I verify that the property named Property is raising its PropertyChanged event, when the setter is called with the string value “new string”. Clean and easy verification of properties on view models. These extension methods can participate in more complex test scenarios:

  1. [Test]
  2. public void OnPropertyChangedIsCalledForParticularProperty()
  3. {
  4.     // Arrange
  5.     var sut = new MyClass();
  6.  
  7.     // Act
  8.     // Assert
  9.     sut.ShouldNotifyOn(model => model.Property)
  10.         .When(model => model.IntProperty = 3);
  11. }

In this test I verify that the Property is raising the event, whenever another property setter is called.

Combining ShouldNotifyOn with combinations of the ShouldNotNotifyOn makes these two small extension methods quite handy when testing and verifying properties on view models.


comments powered by Disqus