Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
You can easily publish PropertyChanged events from the Moq.
public static class MockExtensions
public static IReturnsResult<T> NotifyPropertyChanged<T, TResult>(this Mock<T> mock, Expression<Func<T, TResult>> expression, TResult setupValue) where T : class, INotifyPropertyChanged
var memberExpression = expression.Body as MemberExpression;
if (memberExpression == null) throw new ArgumentException("expression.Body is not MemberExpression");
var returnResult = mock.Setup(expression).Returns(setupValue);
mock.Raise(m => m.PropertyChanged += null, new PropertyChangedEventArgs(memberExpression.Member.Name));
return returnResult;
Copy link

nuitsjp commented Dec 22, 2016

If you are publishing from a Mock PropertyChanged typically describe this way.

sampleModel.Setup(m => m.Value).Returns("NewValue");
sampleModel.Raise(m => m.PropertyChanged += null, new PropertyChangedEventArgs("Value"));

Using this extension method makes you happy.

sampleModel.NotifyPropertyChanged(m => m.Value, "NewValue");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment