Search code examples
c#mstest

Comparing 2 lists of objects ignoring order


Using MSTest V2 and CollectionAssert.AreEquivalent() is failing even if both lists contain the same objects:

// User-defined object
public class ClientDto
{
    public string FullName { get; set; }

    public decimal RentShare { get; set; }
}


// Test method
var expectedShares = new List<ClientDto>
{
    new ClientDto
    {
        FullName = "Harry Potter",
        RentShare = 500m
    },
    new ClientDto
    {
        FullName = "Ron Weasley",
        RentShare = 300m
    },
};
var actualShares = new List<ClientDto>
{
    new ClientDto
    {
        FullName = "Ron Weasley",
        RentShare = 300m
    },
    new ClientDto
    {
        FullName = "Harry Potter",
        RentShare = 500m
    },
};
CollectionAssert.AreEquivalent(expectedShares, actualShares);

Is there anything else that needs to be implemented in the ClientDto?


Solution

  • Because class ClientDto doesn't implement IEquatable<ClientDto> or IEquatable, instances are compared using reference equality.

    Thus, the instances in the list will fail to compare - even though they contain the same data - because their references are different.

    To fix this, just implement IEquatable<ClientDto> so that CollectionAssert.AreEquivalent() can compare the objects correctly:

    public class ClientDto: IEquatable<ClientDto>
    {
        public string FullName { get; set; }
    
        public decimal RentShare { get; set; }
    
        public bool Equals(ClientDto? other)
        {
            if (ReferenceEquals(null, other))
                return false;
            if (ReferenceEquals(this, other))
                return true;
    
            return FullName == other.FullName && RentShare == other.RentShare;
        }
    
        public override bool Equals(object? obj)
        {
            if (ReferenceEquals(null, obj))
                return false;
            if (ReferenceEquals(this, obj))
                return true;
            if (obj.GetType() != this.GetType())
                return false;
            return Equals((ClientDto) obj);
        }
    
        public override int GetHashCode()
        {
            unchecked
            {
                return (FullName.GetHashCode() * 397) ^ RentShare.GetHashCode();
            }
        }
    
        public static bool operator ==(ClientDto? left, ClientDto? right)
        {
            return Equals(left, right);
        }
    
        public static bool operator !=(ClientDto? left, ClientDto? right)
        {
            return !Equals(left, right);
        }
    }
    

    (Implementation courtesy of Resharper.)

    Note that for recent versions of .Net Core and C#, you can use a record instead of a class, because a record implements IEquatable for you (along with a bunch of other things):

    public record ClientDto
    {
        public string  FullName  { get; set; }
        public decimal RentShare { get; set; }
    }