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: Thanks for the fast response! The classes name is _ReferenceEqualityEquivalencyStep_ therefore I would expected it to be always __false__ for value types. Just try this: ``` int a = 2; bool result = object.ReferenceEquals(a,a). ``` The result will always be false. I don't know if ReferenceEqualityEquivalencyStep is used in other test scenarios (where .Equals() is required to get it work properly) :-). However, if you have a look at the uploaded PropertyAssertionSpecs2.cs you will notice that the test will fail with value types as well. A struct can overwrite Equals() too. Best Regards, Daniel

Viewing all articles
Browse latest Browse all 1402

Trending Articles



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