fluentassertions icon indicating copy to clipboard operation
fluentassertions copied to clipboard

ShouldRaisePropertyChangedFor followed by Count behaves unexpectedly (solution here)

Open Ryan-Palmer opened this issue 8 years ago • 8 comments

I wanted to check the number of times RaisePropertyChanged had been raised for a particular property.

I expected to type:

myVm.ShouldRaisePropertyChangedFor(vm => vm.myProperty).Count().ShouldBeEquivalentTo(2);

This failed - the count returned was for ALL RaisePropertyChange events, not the specific one I am testing.

I have written a quick extension method:

public static int SpecificRaisePropertyChangedCount(this IEnumerable<RecordedEvent> value, string propertyName)
{
    return value.Select(r => r.Parameters)
        .SelectMany(s => s)
        .Count(p => p is PropertyChangedEventArgs && (p as PropertyChangedEventArgs).PropertyName == propertyName);
}

so you can now do:

myVm.ShouldRaisePropertyChangedFor(vm => vm.myProperty)
    .SpecificRaisePropertyChangedCount(nameof(myVm.myProperty))
    .ShouldBeEquivalentTo(2);

Sorry I haven't submitted this as a pull request - I'm up to my neck in it and no time to clone the repo etc, but feel free to add it or whatever (or tell me that I could have done it with the existing code easily ^_^ )

Ryan-Palmer avatar Apr 15 '16 11:04 Ryan-Palmer

Thanks for reporting this. I'll take a look at it.

dennisdoomen avatar Apr 16 '16 07:04 dennisdoomen

Note: This behavior is caused by https://github.com/fluentassertions/fluentassertions/issues/337

BrunoJuchli avatar Sep 18 '18 12:09 BrunoJuchli