NotifyPropertyChanged.Verifier 1.1.1

dotnet add package NotifyPropertyChanged.Verifier --version 1.1.1
NuGet\Install-Package NotifyPropertyChanged.Verifier -Version 1.1.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NotifyPropertyChanged.Verifier" Version="1.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NotifyPropertyChanged.Verifier --version 1.1.1
#r "nuget: NotifyPropertyChanged.Verifier, 1.1.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NotifyPropertyChanged.Verifier as a Cake Addin
#addin nuget:?package=NotifyPropertyChanged.Verifier&version=1.1.1

// Install NotifyPropertyChanged.Verifier as a Cake Tool
#tool nuget:?package=NotifyPropertyChanged.Verifier&version=1.1.1

NotifyPropertyChanged.Verifier

Introducing NotifyPropertyChanged.Verifier, a fluent extension of xUnit for testing implementations of INotifyPropertyChanged in ViewModels.

tl;dr

vm.ShouldNotifyOn(vm => vm.PropertyWithNotify)
  .When(vm => vm.PropertyWithNotify = 42);

vm.ShouldNotNotifyOn(vm => vm.PropertyWithoutNotify)
  .When(vm => vm.PropertyWithoutNotify = -1);

Usage

Consider the following ViewModel:

public class ViewModel : INotifyPropertyChanged {
  int backingField;
  string backingField2;

  public int PropertyWithoutNotify { get; set; }

  public int PropertyWithNotify {
      get => backingField;
      set {
        backingField = value;
        OnPropertyChanged();
      }
  }

  public string PropertyWithMultipleNotifies {
      get => backingField2;
      set {
          PropertyWithNotify = int.Parse(value);
          OnPropertyChanged();
      }
  }

  public event PropertyChangedEventHandler PropertyChanged;

  private void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

To test it, create an xUnit-test project and add a NuGet reference to NotifyPropertyChanged.Verifier. It's a .Net Standard 2.0 library and can be used both in .Net Core and the full .NET Framework. The preceding ViewModel can test its implementation of INotifyPropertyChanged doing:

using NotifyPropertyChanged.Verifier;
using Xunit;

namespace Tests {
    public class UnitTests {
        readonly ViewModel vm;

        public UnitTests() => vm = new ViewModel();

        [Fact]
        public void PropertyWithNotify_WillRaiseNotifyEvent() =>
            vm.ShouldNotifyOn(vm => vm.PropertyWithNotify)
              .When(vm => vm.PropertyWithNotify = 42);

        [Fact]
        public void PropertyWithoutNotify_WillNotRaiseNotifyEvent() =>
            vm.ShouldNotNotifyOn(vm => vm.PropertyWithoutNotify)
              .When(vm => vm.PropertyWithoutNotify = -1);

        [Fact]
        public void PropertyWithMultipleNotifies_WillRaiseMultipleNotifyEvents() =>
            vm.ShouldNotNotifyOn(vm => vm.PropertyWithNotify,
                                 vm => vm.PropertyWithMultipleNotifies)
              .When(vm => vm.PropertyWithMultipleNotifies = "42");
    }
}

The library consists of two extension methods on INotifyPropertyChanged, ShouldNotifyOn and ShouldNotNotifyOn which takes 1 or more property expressions as input. These are the properties that should either receive or not receive a NotifyPropertyChanged-event when an Action<T> is called by the When method. This can anything, not only methods or properties on the ViewModel itself.

Inspired by this blogpost.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.1 10,659 4/21/2021
1.1.0 7,019 9/22/2020
1.0.2 8,963 10/28/2019
1.0.1 568 10/23/2019
1.0.0 490 10/23/2019