Search code examples
c#assert

Assert.AreEqual method usage in checking DataTable types


I have gone through threads given but wasn't able to find an implementation for pass case in c# (msdn website has no datatable/generic collection assert usage either)..

code at client not with us (only the 2 datatables are returned to us)

datarow dr1 = datatable1.newrow();
dr1[0] = 345;

datarow dr2 = datatable1.newrow()
dr2[0] = 345;

datatable1.rows.add(dr1); //(1)
datatable2.rows.add(dr2); //(2)

code at our end

Assert.AreEqual(dt1,dt2); //fails!!How to pass this case??

Solution

  • Your version of the AreEqual method is using Assert.AreEqual(Object expected, Object actual) . This means that it will be using Object.Equals() for the comparison.

    Run this code and see what the value is:

    bool areEqual = dr1.Equals(dr2);
    

    This will return false, because they are not the same reference. Look at the documentation for Object.Equals:

    If the current instance is a reference type, the Equals(Object) method tests for reference equality, and a call to the Equals(Object) method is equivalent to a call to the ReferenceEquals method. Reference equality means that the object variables that are compared refer to the same object.

    You need to find a more appropriate way to compare your data. You can use a DataRowComparer to compare the values instead. You can also loop through each of your rows and compare the values yourself. See dotnetperls for an example.