-
I have following xUnit tests: using System;
using Xunit;
namespace RecordEqualityTest;
public record Entity(Guid Id)
{
public virtual bool Equals(Entity? other) => other?.Id == Id;
}
public record EntityWithCode(Guid Id, string Code) : Entity(Id)
{
public virtual bool Equals(EntityWithCode? other) => base.Equals(other);
}
public class UnitTest1
{
private static readonly Guid id = Guid.NewGuid();
private readonly Entity entity = new(id);
private readonly EntityWithCode entityWithCode = new(id, "Test");
[Fact]
public void Test1() => Assert.True(entity.Equals(entityWithCode));
[Fact]
public void Test2() => Assert.True(entityWithCode.Equals(entity));
[Fact]
public void Test3() => Assert.True(entity == entityWithCode);
[Fact]
public void Test4() => Assert.True(entityWithCode == entity);
} I would like to have all tests passing, however test 2 and 4 fails. It turns out that when I tried overriding How do I make proper equality comparison here? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You don't use a record. See Andy's answer here: #5586 (comment). |
Beta Was this translation helpful? Give feedback.
You don't use a record. See Andy's answer here: #5586 (comment).