Skip to content

Object graph comparison is broken when type implements IEnumerable? #860

@jstachowiak

Description

@jstachowiak

Hello!

When a type is a collection the comparison seems to ignore all other type members. Is this by design or am I missing here something? What would be the recommended workaround for now? I'm using version 5.3.2.

public class FluentAssertionsTests
{
    public class CollectionA : List<ObjectA>
    {
        public int Y { get; set; }
    }

    public class ObjectA
    {
        public int X { get; set; }
    }

    [Fact]
    public void ThisFailsAsExpected()
    {
        var a1 = new ObjectA();
        var a2 = new ObjectA();

        a1.X = 1;
        a2.X = 2;

        a1.Should().BeEquivalentTo(a2);
    }

    [Fact]
    public void ThisShouldFailButSucceeds()
    {
        var a1 = new CollectionA {new ObjectA {X = 1}};
        var a2 = new CollectionA {new ObjectA {X = 1}};

        a1.Y = 1;
        a2.Y = 2;

        a1.Should().BeEquivalentTo(a2);
    }
}

Thanks :)

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