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:

?View Code CSHARP
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:

?View Code CSHARP
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.

Share this article:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogosphere News
  • Fleck
  • LinkedIn
  • Live
  • Netvibes
  • Print this article!
  • StumbleUpon
  • Technorati
  • Tumblr
  • TwitThis
  • Yahoo! Buzz
  • BlinkList
  • blogmarks
  • De.lirio.us
  • DZone
  • feedmelinks
  • Furl
  • IndianPad
  • kick.ie
  • LinkaGoGo
  • Linkter
  • MisterWong
  • MySpace
  • Netvouz
  • NewsVine
  • Ping.fm
  • Reddit
  • scuttle
  • Slashdot
  • Socialogs
  • SphereIt
  • Spurl
  • Taggly
  • Webnews.de
  • Wists
  • YahooMyWeb

Tags: , ,

Thursday, March 19th, 2009 TDD No Comments