-
Notifications
You must be signed in to change notification settings - Fork 731
Description
I just noticed an somewhat unexpected behaviour testing events for one of my classes:
Given the following test
[Fact]
public void AddRangeRaisesPropertyChangedEventForItemIndexer()
{
// given
using (var observableDictionary = new ObservableDictionary<int, string>())
{
observableDictionary.ThresholdAmountWhenItemChangesAreNotifiedAsReset = 0;
observableDictionary.MonitorEvents();
var items = new List<KeyValuePair<int, string>>()
{
new KeyValuePair<int, string>(1, "One"),
new KeyValuePair<int, string>(2, "Two"),
new KeyValuePair<int, string>(3, "Three"),
};
// when
observableDictionary.AddRange(items);
// then
observableDictionary
.ShouldRaise(nameof(observableDictionary.PropertyChanged))
.WithSender(observableDictionary)
.WithArgs<PropertyChangedEventArgs>(args => args.PropertyName == "Item[]")
.Should().HaveCount(1);
}
}I get a failing test because Count (the very last line) was expected to be 1, but it is 2.
Now the thing is - exactly one event is being raised for Item[], the second one is for a different property (.Count).
Looking at the source of PropertyChangedSourceExtensions.cs it becomes apparent that currently the IEventRecorder instance is returned back as-is and not a filtered-down version using the supplied predicate.
I was expecting that every .With*() method here checks for the condition and if it passes, it returns the filtered eventRecorder back so one can actually fluently chain up checks.
I.e.:
.ShouldRaise(nameof(observableDictionary.PropertyChanged))checks for any events for the given name, fails if none are raised or if they are, returns a filtered EventRecorder back only containing those events.WithSender(observableDictionary)then checks whether those ("PropertyChanged") events have the given sender, fails if not OR if they do, return a further filtered EventRecorder back - only containing "PropertyChanged" events back with the given sender.WithArgs<PropertyChangedEventArgs>(args => args.PropertyName == "Item[]")- same here: takes the (fluently filtered) stream and checks whether the PropertyName is the expected one, fails if not but if they are, again, returns a further filtered eventRecorder back.. and so on and so on.
Currently it works 'unfiltered' / fluently for apparently all the .With* event related methods there, but is this something worth considering/changing or am I just doing/expecting it wrong? :)
Cheers,
-J