question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fakes that overrides Equals are not considered to be equal to themselves

See original GitHub issue

Similar 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:closed
  • Created 3 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
blairconradcommented, Jun 6, 2020

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.

1reaction
thomaslevesquecommented, Jun 5, 2020

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 for GetHashCode and ToString). 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found