Skip to content

Instantly share code, notes, and snippets.

Created December 22, 2016 10:05
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nuitsjp/7bacd0a3411d2c7b0595917b89f71b03 to your computer and use it in GitHub Desktop.
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