1

I have the following test that surprisingly passes:  

abstract record class Base(string Property1);
sealed record class SubClassA(string Property1) : Base(Property1);
sealed record class SubClassB(string Property1, string Property2) : Base(Property1);
    
[Fact]
public void Test()
{
    var a = new SubClassA("test");
    var b = new SubClassB("test", "test");
    b.Should().BeEquivalentTo(a);
}

In the FluentAssertions documentation it states that by default classes are compared using their members. How can this pass, since SubClassB clearly has one member more than SubClassA? I can make the test pass by changing to value semantics:

b.Should().BeEquivalentTo(a, options => options.ComparingRecordsByValue());

What is going on here? Thanks!

Ynv
  • 1,824
  • 3
  • 20
  • 29

1 Answers1

2

Your assertion is telling FA to make sure that b is equivalent to a, and since SubClassA only has two properties, it will ignore the third property of SubClassB. In other words, the expectation drives the comparison.

Dennis Doomen
  • 8,368
  • 1
  • 32
  • 44
  • Thank you for your response. I'm still confused about the exact definition of equivalence in FA: two objects are considered "equivalent" if the intersection of members of the two objects return the same value? – Ynv May 11 '22 at 08:15
  • 1
    @Ynv They are considered equivalent if all the public members of the expected value are the same as the corresponding members of the actual value. I would expect `Ynv is equivalent to a programmer` to be true, even if `Ynv` is also a father. – Matthew Watson May 11 '22 at 08:25
  • Thanks. Just one small thing: in the example `SubClassA` only has one property? – Ynv May 11 '22 at 08:27