Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing Two objects using Assert.AreEqual()

I 'm writing test cases for the first time in visual studio c# i have a method that returns a list of objects and i want to compare it with another list of objects by using the Assert.AreEqual() method.

I tried doing this but the assertion fails even if the two objects are identical.

I wanted to know if this method, the two parameters are comparing references or the content of the object,

Do I have to overload the == operator to make this work?

like image 686
Vishweshwar Kapse Avatar asked Mar 21 '13 12:03

Vishweshwar Kapse


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.

What does the assert AreEqual method check?

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.

How do you assert two objects in Junit?

assertEquals() calls equals() on your objects, and there is no way around that. What you can do is to implement something like public boolean like(MyClass b) in your class, in which you would compare whatever you want. Then, you could check the result using assertTrue(a. like(b)) .

Can we compare two objects in C#?

In C# objects can be compared with the == operator, with the Equals(Object) member, with the Object. Equals(Object, Object) method or using custom comparators that implement one of or more of the IEquatable<T> , IComparable , IStructuralEquatable or IStructuralComparable interfaces. There's also a Object.


2 Answers

These answers are far too complicated for the issue. There are no overrides necessary to compare two Lists, and you do not need to break out multiple asserts. Microsoft uses the following class, CollectionAssert.

CollectionAssert.AreEqual(expectedList, actualList) 

This works for Lists, Dictionaries, and whatever else implements ICollection interface.

The microsoft documentation is at the following location and details the various types of assertions which can be made on collections

http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.collectionassert.aspx

However, as mentioned by @Bart, this does not work as expected on Lists of (complex) Objects, and the Equals method may still need to be overwritten for those cases.

like image 109
a-1 Avatar answered Oct 20 '22 00:10

a-1


If you are using NUnit this is what the documentation says

Starting with version 2.2, special provision is also made for comparing single-dimensioned arrays. Two arrays will be treated as equal by Assert.AreEqual if they are the same length and each of the corresponding elements is equal. Note: Multi-dimensioned arrays, nested arrays (arrays of arrays) and other collection types such as ArrayList are not currently supported.

In general if you are comparing two objects and you want to have value based equality you must override the Equals method.

To achieve what you are looking for try something like this:

class Person  {     public string Firstname {get; set;}     public string Lastname {get; set;}       public override bool Equals(object other)      {       var toCompareWith = other as Person;       if (toCompareWith == null)          return false;       return this.Firstname ==  toCompareWith.Firstname &&            this.Lastname ==  toCompareWith.Lastname;      } }   

and in your unit test:

Assert.AreEqual(expectedList.ToArray(), actualList.ToArray()); 
like image 38
anouar.bagari Avatar answered Oct 20 '22 00:10

anouar.bagari