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.