bniwredyc
bniwredyc

Reputation: 8829

PropertyChanged event testing: is this a good way?

I'm developing WPF applications using MVVM pattern. I have ViewModel with code like this:

public bool EditModeEnabled
{
    get { return _EditModeEnabled; }
    set
    {
        _ModeEditModeEnabled = value;
        OnPropertyChanged("EditModeEnabled");
        OnPropertyChanged("CommentTextBoxVisibility");
    }
}

OnPropertyChanged is virtual method of base class which just raise PropertyChanged event. I want to test PropertyChanged event raising and there my test method:

public void EditModeEnabledTest()
{
    var imageViewModel = TestHelper.GetTestImageViewModel();
    var firedEvents = new List<string>();
    imageViewModel.PropertyChanged += ((sender, e) => firedEvents.Add(e.PropertyName));
    imageViewModel.Mode = true;
    Assert.AreEqual(firedEvents.Count, 2);
    Assert.IsTrue(firedEvents.Contains("EditModeEnabled"));
    Assert.IsTrue(firedEvents.Contains("CommentTextBoxVisibility"));
    ...
}

Is it a good way to test ProprtyChanged event?

Upvotes: 12

Views: 4489

Answers (3)

ecth
ecth

Reputation: 1245

Similar to the suggestion of Mark Seemann, with Fluent Assertions you have simple nice methods that wrap it all up for you, like:

subject.Should().Raise().PropertyChangeFor(x => x.SomeProperty);

Source https://fluentassertions.com/eventmonitoring/

Upvotes: 1

jbe
jbe

Reputation: 7031

I believe it’s a good idea to unit test the PropertyChanged event in the example you have shown. You might have written the property name string wrong which would result in a missing update.

With the WPF Application Framework (WAF) it’s very easy to write such a unit test:

Person person = new Person();
AssertHelper.PropertyChangedEvent(person, x => x.Name, () => person.Name = "Luke");

Upvotes: 1

Mark Seemann
Mark Seemann

Reputation: 233170

I use a little Fluent API for doing exactly that. It allows you to write tests like this:

var imageViewModel = TestHelper.GetTestImageViewModel();
imageViewModel.ShouldNotifyOn(s => s.EditModeEnabled)
    When(s => s.Mode = true);

Besides being succinct, I prefer this approach because it's type-safe - no string values to keep in sync with your API.

To test that the event is being raised for more than one property, you can just write another test that does this. This will give you many tests, but each will be very small and you avoid Assertion Roulette.

Upvotes: 13

Related Questions