Skip to content

EventRaisingExtensions WithArgs matches arguments with wrong type #1915

@enzo-ae

Description

@enzo-ae

Description

When using the WithArgs method in EventRaisingExtensions, all occured events are searched for a parameter of the given type. Once a matching parameter is found, all following occured events are added to the eventsMatchingPredicate, even if they don't have a matching parameter of the given type

Complete minimal example reproducing the issue

Complete means the code snippet can be copied into a unit test method in a fresh C# project and run.
Minimal means it is stripped from code not related to reproducing the issue.

E.g.

using System;
using FluentAssertions;
using FluentAssertions.Events;
using Xunit;

public class A
{
    public event EventHandler<object> Event;

    public void OnEvent(object o)
    {
        Event.Invoke(nameof(A), o);
    }
}

public class B {}

public class C {}

public class FluentAssertionsEventRaisingExtensions
{
    [Fact]
    public void Test()
    {
        A a = new A();
        using var aMonitor = a.Monitor();
        
        a.OnEvent(new B());
        a.OnEvent(new C());

        IEventRecording filteredEvents = aMonitor.GetRecordingFor(nameof(A.Event)).WithArgs<B>();
        filteredEvents.Should().HaveCount(1);
    }
}

Expected behavior:

bool hasArgumentOfRightType gets reset at the end of the foreach loop

Actual behavior:

Once any parameter matches the type, all following following events are added to the filtered recording.

Versions

  • Which version of Fluent Assertions are you using?
    6.6.0
  • Which .NET runtime and version are you targeting? E.g. .NET framework 4.6.1 or .NET Core 2.1.
    .NET 6

Additional Information

Any additional information, configuration or data that might be necessary to reproduce the issue.

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