![]() |
Languages »
C# »
Utilities
Intermediate
License: The Code Project Open License (CPOL)
Testing Equality of Two ObjectsBy Carl JohansenA general purpose C# method for testing whether two objects have the same values in their respective public properties |
C# (C# 2.0), .NET (.NET 2.0), Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
A while ago, I was looking for a way to compare two objects as part of a unit test. I didn't want Assert.AreSame() because I didn't just want to see if they were the same object, and I didn't want Assert.AreEqual() because I didn't want to have to override Equals(). Ideally I wanted a general utility that would compare any two objects and say whether they were equal. I decided to define “equal” as “having the same value for all public properties”. I wanted a deep comparison, so if a public property is another object then all of its public properties should also be checked.
My search led me to the AssertGraphPropertiesEqual function written by Keith Brown. This was just the kind of thing I had in mind, but in using it I made a number of changes that I think made it generally more useful. I would like to share my version with you. These are the main changes in my version:
AssertPublicPropertiesEqual, but feel free to change that name if you don't like it. List<string> objects that contain the same strings to be equal even though their Capacity property values are different. String.Chars). Normally these will be covered by the check of some other non-indexed property. pi.PropertyType.IsAssignableFrom(typeof(IEnumerable)). That seemed to be the wrong way around, so I changed it to typeof(IEnumerable).IsAssignableFrom(objectType). The method is intended to be used in unit testing, so it uses standard Asserts to perform the comparisons. I have tried it with both Visual Studio Team System (VSTS) and NUnit. Just include the appropriate using statement for the library that you use.
A typical call would be something like this:
MyClass expectedObject = the expected result of the test;
MyClass actualObject = MethodBeingTestedThatReturnsAMyClassObject();
UnitTestingHelper.AssertPublicPropertiesEqual(expectedObject, actualObject);
As I mentioned above, you can specify properties to be ignored during the comparison. Here's how to ignore the Capacity property of List<string> objects:
MyClass expectedObject = the expected result of the test;
MyClass actualObject = MethodBeingTestedThatReturnsAMyClassObject();
IgnoreProperties ignoreProps = new IgnoreProperties();
ignoreProps.Add(new PropertyComparisonExclusion(typeof(List<string>), "Capacity"));
UnitTestingHelper.AssertPublicPropertiesEqual
(expectedObject, actualObject, ignoreProps);
When defining PropertyComparionExclusions, you can use the typeAction property to specify whether to ignore the property only on the specified type, or on the type and all its derived types. The default is PropertyComparisonExclusionTypeAction.MatchExactType, meaning that the property is ignored only on the specified type.
I have two questions:
I've thrown various objects at it, but the possible inputs are literally infinite so please let me know if you find a problem with it.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 13 Jan 2008 Editor: Deeksha Shenoy |
Copyright 2008 by Carl Johansen Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |