0

Consider the following C# code:

class Parent
{
    public int Id { get; set; }
    public Child Child { get; set; }
}

class Child
{
    public int Id { get; set; }
}

var p1 = new Parent { Id = 0, Child = new Child { Id = 1 } };
var p2 = new Parent { Id = 0, Child = new Child { Id = 2 } };

p1.Should().BeEquivalentTo(p2);

This fails, as expected, with the message

Expected property p1.Child.Id to be 2, but found 1.

Now we replace the last line with

p1.Should().BeEquivalentTo(p2, o => o.ExcludingNestedObjects());

but it still fails with the message

Expected property p1.Child to be Child
{
    Id = 2
}, but found Child
{
    Id = 1
}

Help text for ExcludingNestedObjects says Causes the structural equality check to exclude nested collections and complex types. Child is a complex type, why does it still get checked?

I can make the assertion pass by saying

p1.Should().BeEquivalentTo(p2, o => o.Excluding(p => p.Child));

but if I have a lot of complex types, I do not want to exclude them all one by one.

Andrew
  • 1,139
  • 1
  • 12
  • 27

1 Answers1

0

I also raised a bug in the FluentAssertions repo on Github, and got an answer there. ExcludingNestedObjects excludes complex types from STRUCTURAL equality check (as the help text suggests), but does not exclude them from equality check completely. I.e. they will still be compared, but using their default Equals implementation rather than comparing their property structure: https://github.com/fluentassertions/fluentassertions/issues/2210

Andrew
  • 1,139
  • 1
  • 12
  • 27