Assert to compare two lists of objects C#

前端 未结 3 789
梦谈多话
梦谈多话 2021-01-14 17:13

I am currently trying to learn how to use unit testing, and I have created the actual list of 3 animal objects and the expected list of 3 animal objects. The question is how

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-14 17:42

    Just incase someone comes across this in the future, the answer was I had to create an Override, IEqualityComparer as described below:

    public class MyPersonEqualityComparer : IEqualityComparer
    {
    public bool Equals(MyPerson x, MyPerson y)
    {
        if (object.ReferenceEquals(x, y)) return true;
    
        if (object.ReferenceEquals(x, null)||object.ReferenceEquals(y, null)) return false;
    
        return x.Name == y.Name && x.Age == y.Age;
    }
    
    public int GetHashCode(MyPerson obj)
    {
        if (object.ReferenceEquals(obj, null)) return 0;
    
        int hashCodeName = obj.Name == null ? 0 : obj.Name.GetHashCode();
        int hasCodeAge = obj.Age.GetHashCode();
    
        return hashCodeName ^ hasCodeAge;
    }
    

    }

提交回复
热议问题