Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the test 'Assert.AreEqual' has failed when I compare two empty list?

I have a class MyCustomClass:

public MyCustomClass
{
    public MyCustomClass()
    {
        MyObject = new List<MyCustomObject>();
    }

    public List<MyCustomObject> MyObject {get; set;}
}

In the Test:

List<MyCustomObject> aux = new List<MyCustomObject>();
MyCustomClass oClass = new MyCustomClass();
Assert.AreEqual(aux, oClass.MyObject)

The test has failed, why? Every property, static member, etc are the same.

like image 890
Erick Asto Oblitas Avatar asked Sep 26 '12 16:09

Erick Asto Oblitas


People also ask

How do you compare two objects in assert?

The assert. deepEqual() method tests if two objects, and their child objects, are equal, using the == operator. If the two objects are not equal, an assertion failure is being caused, and the program is terminated. To compare the objects using the === operator, use the assert.

How do you assert AreEqual in C#?

AreEqual(Object, Object, String, Object[])Tests whether the specified objects are equal and throws an exception if the two objects are not equal. Different numeric types are treated as unequal even if the logical values are equal. 42L is not equal to 42.


1 Answers

In this case Assert.AreEqual will check to see if the two objects are the same, and they're not. You should use CollectionAssert.AreEqual instead, which will return true if the two "have the same elements in the same order and quantity."

like image 71
Reed Copsey Avatar answered Sep 18 '22 08:09

Reed Copsey