/// <summary> /// Asserts that the specified object raises the <see cref="INotifyPropertyChanged.PropertyChanged"/> event for the specified property. /// </summary> /// <typeparam name="TObject">The <see cref="Type">type</see> of <see cref="INotifyPropertyChanged">object</see> to test.</typeparam> /// <typeparam name="TValue">The <see cref="Type">type</see> of value to assign to the property.</typeparam> /// <param name="assert">The extended <see cref="Asserter"/>.</param> /// <param name="subject">The <see cref="INotifyPropertyChanged">object</see> to test.</param> /// <param name="testProperty">The <see cref="Expression{T}"/> representing the test property.</param> /// <param name="value">The value to to assign to the tested property.</param> /// <param name="otherProperties">A sequence of other property <see cref="Expression{T}">expressions</see> representing the additional properties /// that are expected to change and raise the <see cref="INotifyPropertyChanged.PropertyChanged"/> event as a result of changing the /// <paramref name="testProperty">tested property</paramref>.</param> /// <include file="examples.xml" path="Types/Type[@name='INotifyPropertyChangedExtensions']/Member[@name='PropertyChanged`2']/example" /> public static void PropertyChanged <TObject, TValue>( this Asserter assert, TObject subject, Expression <Func <TObject, TValue> > testProperty, TValue value, params Expression <Func <TObject, object> >[] otherProperties) where TObject : INotifyPropertyChanged { Arg.NotNull(assert, nameof(assert)); assert.AssertParameterIsNotNull(subject, nameof(subject)); assert.AssertParameterIsNotNull(testProperty, nameof(testProperty)); assert.AssertParameterIsNotNull(otherProperties, nameof(otherProperties)); var test = subject.GetTestForProperty(assert, testProperty, out var propertyName); var unexpected = new HashSet <string>(); var properties = otherProperties.ToDictionary(assert); properties.Add(propertyName, false); void OnPropertyChanged(object sender, PropertyChangedEventArgs e) { if (properties.ContainsKey(e.PropertyName)) { properties[e.PropertyName] = true; } else { unexpected.Add(e.PropertyName); } } subject.PropertyChanged += OnPropertyChanged; try { test(subject, value); } finally { subject.PropertyChanged -= OnPropertyChanged; } assert.AllPropertiesWereChanged(properties); assert.UnexpectedPropertiesWereNotChanged(unexpected); }