Skip to content

Show the values of properties that are collections when comparing equivalence of collections of anonymous objects #2140

@benagain

Description

@benagain

Description

When asserting equivalency of a collection of anonymous objects (using ContainsEquivalentOf or BeEquivalentTo) that contain properties which are themselves collections, the failure message does not report the contents of those properties.

The first test failure message (of the reproducible example below) shows the values of the collection in the property of both the expected and actual objects.

The only difference in the second test is that the objects are anonymous. Here the failure message does not show the values of the collections, instead it shows the collections' types.

I would be great if FluentAssertions could show the collections' values for anonymous objects the same way it does for named typed objects.

Reproduction Steps

using FluentAssertions;

public class X
{
    public List<string> Collection { get; set; }
}

public class UnitTest1
{
    [Fact]
    public void ContainsEquivalentWhenTyped()
    {
        var actual = new[]
        {
            new X
            {
                Collection = new List<string> { { "alhpa" } }
            }
        };

        var expected = new X
        {
            Collection = new List<string> { { "beta" } }
        };

        actual.Should().ContainEquivalentOf(expected);
        //  Expected actual {
        //      X
        //      {
        //          Collection = {"alhpa"}
        //      }
        //  }
        //   to contain equivalent of X
        //  {
        //      Collection = {"beta"}
        //  }.

    }

    [Fact]
    public void ContainsEquivalentWhenAnonymous()
    {
        var actual = new[]
        {
            new
            {
                Collection = new List<string> { { "alhpa" } }
            }
        };

        var expected = new
        {
            Collection = new List<string> { { "beta" } }
        };

        actual.Should().ContainEquivalentOf(expected);
        //   Expected actual {{ Collection = System.Collections.Generic.List`1[System.String] }}
        //    to contain equivalent of { Collection = System.Collections.Generic.List`1[System.String] }.
    }
}

Expected behavior

ContainEquivalentOf should produce a message containing the values of the collection

Expected actual {{ Collection = {"alpha"} }} to contain equivalent of { Collection = {"beta"} }.

Actual behavior

Expected actual {{ DictionaryProperty = System.Collections.Generic.List`1[System.String] }} to contain equivalent of { DictionaryProperty = System.Collections.Generic.List`1[System.String] }.

With configuration:
- Use declared types and members
- Compare enums by value
- Compare tuples by their properties
- Compare anonymous types by their properties
- Compare records by their members
- Include non-browsable members
- Match member by name (or throw)
- Be strict about the order of items in byte arrays
- Without automatic conversion.

Regression?

No response

Known Workarounds

No response

Configuration

net6.0
FluentAssertions v6.10.0

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions