You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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
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
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.
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.
Scenario:
Test
The aggregateRootObject.GetHashCode() returns 0. The debug control doesn't even reach GetHashCode() method body.
The text was updated successfully, but these errors were encountered: