views:

101

answers:

3

I can't understand why NUnit Assert.AreEqual is failing.

var dataService = new DataService(db);
dataService.Set("Tests", "circle1", circle);
var circleData = dataService.Get("Tests", "circle1");
Assert.IsNotNull(circleData);
var circleCopy = circleData.Get();
Assert.AreEqual(circle, circleCopy);

Using NHibernate (storing serialized data, then deserializing it). I've inserted a breakpoint and inspected the objects in Local variables window -- they are identical.

Here is the NUnit message:

Assert.AreEqual failed. Expected:<TestData.TestClassCircle>. Actual:<TestData.TestClassCircle>.

Why would this test be failing when the objects appear to be identical?

+11  A: 

What is CircleData? Does it override Equals? Looks to me like you're cloning it, which means that you've got two separate CircleData instances. Unless CircleData overrides Equals, then it will be performing a reference equality check, which will fail.

HTH,
Kent

Kent Boogaart
Been bitten by this about 3 times myself. Now I always remember :)
Merlyn Morgan-Graham
A: 

It is not clear what is this dataService of yours doing behind the scenes but if it is serializing/deserializing you won't get same object references. AreEqual compares object references in memory and not values.

Darin Dimitrov
A: 

Probably your Object doesn't implement Object.Equals correctly.

http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx

oefe