DataSet
TDD: Comparing Datasets
I’ve been writing some unit tests today to make sure that my code fetches the correct DataSet from the database. I wanted to test that the DataSet returned by the data layer matches the DataSet that I expect, so I used the following Assert statement:
Assert.AreEqual(expectedDataSet, actualDataSet); |
When I ran this test on two empty DataSets it failed with this helpful message:
NUnit.Framework.AssertionException:
Expected: <system.Data.DataSet>
But was: <system.Data.DataSet>
That suggests that while the structure and data inside two DataSets are the same, there is a fundamental difference in the objects that means you cannot compare them in this way.
It turns out that there is a quick and easy way to compare two DataSets if you want to ensure that they’re identical. Here it is:
Assert.AreEqual(expectedDataSet.GetXml(), actualDataSet.GetXml()); |
By comparing the XML serialised output of both DataSets we can ensure that the structure and data are identical.































