NUnit Assert.AreEqual(object a, object b) false ne

2019-08-21 21:27发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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

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