0

I have a two Lists which contains two User objects inside.

Objects inside lists are same for their values.

But when i am checking this lists Unit test return CollectionAssert.AreEqual failed. (Element at index 0 do not match).

I think CollectionAssert.AreEqual method does not checking list for their values, it checking for the reference.

My question is how can i compare two list in Unit test for their value?

public class User 
{
    public string Name { get; set; }
    public string Password { get; set; }
}

[TestMethod]
public void CheckUserList()
{
    List<User> users = new List<User>()
    {
        new User(){ Name="Name", Password="Pass" },
        new User(){ Name="Name1", Password="Pass1" },
        new User(){ Name="Name2", Password="Pass2" },
    };

    List<User> users1 = new List<User>()
    {
        new User(){ Name="Name", Password="Pass" },
        new User(){ Name="Name1", Password="Pass1" },
        new User(){ Name="Name2", Password="Pass2" },
    };

    CollectionAssert.AreEqual(users, users1);
}
MikeLimaSierra
  • 799
  • 2
  • 11
  • 29
  • #1 override `User.Equals()` #2 `users.SequenceEquals(users1)` – fubo Nov 29 '18 at 06:38
  • You'll want to either override Equals and GetHashCode in class User, or provide an implementation of IComparer to pass into an overload of CollectionAssert.AreEqual – Anthony Pegram Nov 29 '18 at 06:38
  • Possible duplicate of [Assert to compare two lists of objects C#](https://stackoverflow.com/questions/19562505/assert-to-compare-two-lists-of-objects-c-sharp) – currarpickt Nov 29 '18 at 06:38
  • thanks fubo and Anthony Pegram overriding Equals and GetHashCode is enough. – muradheydarov Nov 29 '18 at 06:41

2 Answers2

1

I think you need to override == operator or Equals() method for your User class.

public class User
{
    public string Name { get; set; }
    public string Password { get; set; }

    public override bool Equals(object obj)
    {
        if (obj is User another)
        {
            return Name == another.Name && Password == another.Password;
        }

        return base.Equals(obj);
    }

    public static bool operator ==(User left, User right)
    {
        return left.Equals(right);
    }

    public static bool operator !=(User left, User right)
    {
        return !left.Equals(right);
    }

}
ekvalizer
  • 130
  • 1
  • 9
0

It is enough Equal and GetHashCode methods. Then CollectionAssert.AreEqual will work as you expected

public class User 
{
    public string Name { get; set; }
    public string Password { get; set; }

    public override bool Equals(object obj)
    {
        var user = obj as User;
        return user != null &&
               Name == user.Name &&
               Password == user.Password;
    }

    public override int GetHashCode()
    {
        var hashCode = 1290039854;
        hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Name);
        hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Password);
        return hashCode;
    }
}