Fakes that overrides Equals are not considered to be equal to themselves
See original GitHub issueSimilar to #1630, I found an odd behavior and couldn’t find any info on it -
class MyClass {
public override bool Equals(object o) { ... } // calls to base
public override int GetHashCode() { ... } // calls to base
}
var myClass = A.Fake<MyClass>();
myClass.Should().Be(myClass); // this fails.
This seems odd to me as they are the same object. Can someone explain if it is the intended behavior or is it a bug?
I know that CallsBaseMethods()
can solve this problem, but it is annoying to call it on every fake I create.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Java equals() method always returning false
The equals method does not magically test whether two instances of your custom class are considered "equal" to eachother. You must define when ......
Read more >Why is the hashCode method usage of HashSet not ...
This class overrides equals(Object) , but does not override hashCode() . Therefore, the class may violate the invariant that equal objects ...
Read more >Overriding equals method in Java
We can override the equals method in our class to check whether two objects have same data or not. Java ...
Read more >Overriding equals() and hashCode() method in Java ...
1) Reflexive: Object must be equal to itself. ... 2) If two objects are not equal by equals() method then there hashcode could...
Read more >In Java programming, when do we have to override equals ...
The equals method will treat both objects as equal. This should result in only one of those items being included in the Set,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think the creation of #1777 resolves this issue, so I’m going to close it. Thanks for bringing the peculiar behaviour to our attention, @afifit! If I’m wrong, and you do have more to discuss here, we’d be happy to reopen the issue.
Hi @afifit,
@blairconrad and I discussed this further, and we came to the conclusion that the current behavior probably isn’t useful to anyone, so we’re going to change it (for
Equals
, but also forGetHashCode
andToString
). I opened a new issue to track this (#1777).Note that the fake still won’t have the overridden behavior (to get that behavior, use
CallsBaseMethod
as you’ve been doing), but at least a fake will be equal to itself.