Skip to content

ShouldRaisePropertyChangedFor followed by Count behaves unexpectedly (solution here) #393

@Ryan-Palmer

Description

@Ryan-Palmer

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 ^_^ )

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions