Compare .NET objects or Why Assert.AreEqual fails for complex objects.

MS Unit Test and NUnit have method Assert.AreEqual(object o1, object o2). What they can do - is simply compare integers or strings, etc. But they do not compare complex object.
I have used different code, but a lot of them do not correctly compare really complex objects(Like object with nested object, or list, or enum).
The solution is to use http://comparenetobjects.codeplex.com/ C# class. It is pretty simple one .cs file and it works!

 public static void CompareObjectValues(object o1, object o2, IEnumerable<string> elementsToIgnore)  
     {  
       var compareObjects = new CompareObjects();  
       compareObjects.MaxDifferences = 100;  
       compareObjects.ElementsToIgnore.AddRange(elementsToIgnore);  
       var comp = compareObjects.Compare(o1, o2);  
       if (comp == false)  
       {  
         Assert.Fail(compareObjects.DifferencesString);  
       }  
       Assert.IsTrue(comp);  
     }  

Comments

  1. All the time I used to study article in news papers but now as I am a user of web so from now I am using net for articles or reviews, thanks to web.

    www.gofastek.com

    ReplyDelete

Post a Comment

Popular posts from this blog

UML Sequence Diagram and Visual Studio