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

Commented 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: Hmm, so I'm wondering now what would happen if you do that with primitive types. An integer of 2 in one type might not be 'reference equal' with another integer with value 2. Maybe it should only use Equals for primitive types. For non-primitive types, it should still use ReferenceEquals. Any ideas?

Viewing all articles
Browse latest Browse all 1402

Trending Articles