Skip to content

Should().ContainEquivalentOf() doesn't work with Dictionary properties #2328

@danclarke

Description

@danclarke

Description

When comparing an object with a Dictionary property, BeEquivalentTo() works as expected. However, if the object is within a list, ContainEquivalentOf() does not match correctly.

Reproduction Steps

using FluentAssertions;

namespace FluentAssertionsFail;

public readonly record struct ExampleRecord(
    string Name,
    Dictionary<string, string> Properties
);

public class Tests
{
    [Fact]
    public void DirectTest()
    {
        var first = new ExampleRecord("test", new Dictionary<string, string>{{"key1", "val1"}});
        var second = new ExampleRecord("test", new Dictionary<string, string>{{"key1", "val1"}});
        
        first.Should().BeEquivalentTo(second);
    }
    
    [Fact]
    public void EnumerableTest()
    {
        var list1 = new[]
        {
            new ExampleRecord("test", new Dictionary<string, string> { { "key1", "val1" } }),
            new ExampleRecord("test", new Dictionary<string, string> { { "key1", "val1" } })
        };
        
        var list2 = new[]
        {
            new ExampleRecord("test", new Dictionary<string, string> { { "key1", "val1" } }),
            new ExampleRecord("test", new Dictionary<string, string> { { "key1", "val1" } })
        };

        list1.Should().ContainEquivalentOf(list2);
    }
}

Expected behavior

Both tests should pass.

Actual behavior

Only DirectTest() passes, EnumerableTest() fails with:

Expected list1 
{
    FluentAssertionsFail.ExampleRecord
    {
        Name = "test", 
        Properties = {["key1"] = "val1"}
    }, 
    FluentAssertionsFail.ExampleRecord
    {
        Name = "test", 
        Properties = {["key1"] = "val1"}
    }
} to contain equivalent of 
{
    FluentAssertionsFail.ExampleRecord
    {
        Name = "test", 
        Properties = {["key1"] = "val1"}
    }, 
    FluentAssertionsFail.ExampleRecord
    {
        Name = "test", 
        Properties = {["key1"] = "val1"}
    }
}.

Regression?

No response

Known Workarounds

No response

Configuration

FluentAssertions: 6.12.0
.NET: 6.0

Other information

No response

Are you willing to help with a pull-request?

No

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions