views:

145

answers:

5

I am fairly new to unit testing and am using VS2010 to develop in and run my tests. I have a simple test, illustrated below, that simply compares 2 System.Data.DataTableReader objects. I know that they are equal as they are both created using the same object types, the same input file and I have verified that the objects "look" the same.

I realize I may be dealing with a couple of issues, one being whether or not this is the proper use of Assert.AreEqual or even the proper way to test this scenario, and the other being the main issue I am dealing with which is why this test fails with this exception:

Failed 00:00:00.1000660 0 Assert.AreEqual failed. 
Expected:<System.Data.DataTableReader>. Actual:<System.Data.DataTableReader>. 

Here is the unit test code that is failing:

public void EntriesTest()
{
    AuditLog target = new AuditLog(); 

    target.Init();

    DataSet ds = new DataSet();
    ds.ReadXml(TestContext.DataRow["AuditLogPath"].ToString());
    DataTableReader  expected = ds.Tables[0].CreateDataReader();
    DataTableReader actual = target.Entries.Tables[0].CreateDataReader();
    Assert.AreEqual<DataTableReader>(expected, actual);
}

Any help would be greatly appreciated!

A: 

Be default the check is doing reference equality; they have to refer to the same object. I'd suggest changing your test to check that the reader returns the same values.

tvanfosson
+1  A: 

What you are actually doing is checking whether the 2 DataReaders have the same reference, which they do not since they are two different instances.

Perhaps create a helper method that goes through the DataTables and compares each row, or as others have noted use CollectionAssert.

Mitch Wheat
A: 

I'm guessing equality doesn't work like that with data readers. If you want to compare two records, you need to do something like:

Assert.AreEqual(expected.FieldCount, actual.FieldCount);
for(int i = 0; i < expected.FieldCount; i++)
{
    Assert.AreEqual(expected[i], actual[i]);
}
Matt
[`CollectionAssert.AreEqual`](http://msdn.microsoft.com/en-us/library/ms243763.aspx) does this.
Randolpho
+3  A: 

As far as I can tell, DataTableReader does not override the Equals method; therefore, calling Equals will be a reference equality check; two separate references, failed equality.

What you want are collection assertions. Call CollectionAssert.AreEquivalent() instead. If order is important, use CollectionAssert.AreEqual().

Randolpho
This is exactly what I was looking for--Thanks!
Tom Miller
A: 

I'm guessing you are trying to test that the data that comes from the reader is the same, and not that the readers themselves are the same. I expect this is resulting in an object comparison (that the objects are the same in memory object), and the result will obviously be false.

Try getting the data and comparing that.

NickLarsen