Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moq Object returns 0 for the overridden GetHashCode() present in BaseClass. #1312

Closed
yasirthite opened this issue Dec 13, 2022 · 2 comments
Closed
Labels

Comments

@yasirthite
Copy link

Scenario:

 public abstract class BaseEntity
    {
        public Guid Id { get; protected set; }
        
        protected BaseEntity(Guid id)
        {
            Id = id;
        }
        
        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }
    }
 public abstract class BaseAuditableEntity : BaseEntity
    {
        protected BaseAuditableEntity(Guid id) : base(id)
        {
        }
    }
public abstract class AggregateRoot : BaseAuditableEntity
    {
        protected AggregateRoot(Guid id) : base(id)
        {
        }
    }

Test

[Fact]
        public void GetHashCode_ShouldReturnCorrectHashCode()
        {
            // arrange
            var id = Guid.NewGuid();
            var expectedHashCode = id.GetHashCode();
            
            var aggregateRootObject = new Mock<AggregateRoot>(id).Object;

            // act
            var actualHasCode = aggregateRootObject.GetHashCode();

            // assert
            actualHasCode.Should().Be(expectedHashCode);
        }

The aggregateRootObject.GetHashCode() returns 0. The debug control doesn't even reach GetHashCode() method body.

@yasirthite yasirthite changed the title Moq Object returns 0 for GetHasCode() present in BaseClass. Moq Object returns 0 for overridden GetHashCode() present in BaseClass. Dec 13, 2022
@yasirthite yasirthite changed the title Moq Object returns 0 for overridden GetHashCode() present in BaseClass. Moq Object returns 0 for the overridden GetHashCode() present in BaseClass. Dec 13, 2022
@stakx stakx added the question label Dec 13, 2022
@stakx
Copy link
Contributor

stakx commented Dec 13, 2022

Either set your mock's CallBase property to true so it uses the base class' implementations by default, or (more granularly:) create a setup specifically for the GetHashCode method and use the .CallBase() setup verb to defer to the base class' implementation, but just for that one method.

@stakx stakx closed this as completed Dec 13, 2022
@yasirthite
Copy link
Author

yasirthite commented Dec 15, 2022

Thank you @stakx, it does return a non-zero value now but it calls GetHashCode of the very base class (Object Class) instead of my BaseEntity class I suppose.

The execution control doesn't hit BaseEntity's GetHashCode method.

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }

@devlooped devlooped locked and limited conversation to collaborators Sep 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants