Quantcast
Channel: Fluent Assertions
Viewing all articles
Browse latest Browse all 1402

Closed Issue: PropertyAssertion does not throw if a class overwrites Equals(object obj) [12475]

$
0
0
PropertyAssertions do not work properly if a class overwrites the Equals-method:

```
class ClassThatOverwritesEquals
{
public int Age { get; set; }

public override bool Equals(object obj) {
return true;
}
}
```

This will never throw if you compare two instances of the same type:

```
var subject = new ClassThatOverwritesEquals {
Age = 36,
};

var expectation = new ClassThatOverwritesEquals {
Age = 37,
};

Action act = () => subject
.ShouldHave()
.AllProperties()
.EqualTo(expectation, "because {0} are the same", "they");
```

I would guess the problem is in ReferenceEqualityEquivalencyStep.cs line 43:

```
return !ReferenceEquals(context.Subject, null) && context.Subject.Equals(context.Expectation);
```

The Equals(..) should be replaced by object.ReferenceEquals(..).


Comments: During dogfooding 2.1 I came to the conclusion that I have to roll back this change. Other than using the Equals() implementation, there's no reliable way for detecting whether I should continue traversing an object graph. If an object overrides Equals() it is essentially stating that it behaves like a value type (even though it still is a class).

Viewing all articles
Browse latest Browse all 1402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>