0

i have a .NET class in which i implemented GetHashCode and Equals.

while i believe i covered all properties and have tests for these functions, there could be a case in the future that someone adds a property and forgets to add it to the GetHashCode and Equals functions. forgetting these function will result in the Equals function returning true for objects that differ by the new property.

I am looking for some tool or code that i can use in the testing code, that will loop through all existing properties of an object and test if something was forgotten.

the unit test will look something like this:

for each property in TargetType
    dim instance1 as new TargetType
    dim instance2 as new TargetType
    instance1.property=1
    instance2.property=2
    Assert.AreNotEqual(instance1,instance2)
next

of course the property assignment should be smart to use the correct data type

Dani Avni
  • 423
  • 5
  • 14
  • Even if the above code were to work for `Equals`, `GetHashCode` is going to be considerably trickier (given that two unequal objects may have the same hash code) – Damien_The_Unbeliever May 03 '13 at 06:28
  • even catching a missed property on Equals is better than not catching it at all. I can hope that the fix will also include the GetHashCode – Dani Avni May 03 '13 at 09:38

1 Answers1

0

If you are concerned about new properties being added to your class, I suggest serializing the objects first and then comparing the resulting strings.

This will ensure that all properties are always considered for comparison in the application and in your unit tests.

The serializing helper method would look something like below (I referred this SO thread)

'Meant to be an extension method
Public Shared Function SerializeObject(Of T)(ByVal input As T) As String
        Dim xmlSerializer = New XmlSerializer(input.GetType())
        Dim textWriter = New StringWriter()
        xmlSerializer.Serialize(textWriter, input)
        Return textWriter.ToString()
End Function

You could use the above method in your custom Equals method. Hope this helps.

Community
  • 1
  • 1
Channs
  • 2,091
  • 1
  • 15
  • 20
  • That's funny looking VB :-) – Damien_The_Unbeliever May 03 '13 at 06:26
  • @Damien_The_Unbeliever - I am more comfortable with C#, so posted that first. Now, I have changed it to VB.NET :o) – Channs May 03 '13 at 06:27
  • so if the equals returns true but the XML serialization is different then I know equals is missing something. nice idea! but as I wrote, I am looking for a tool/code that will do this and will handle all properties. the bottom line is something still needs to assign different values to the same property on two instances and do some kind of matching (be it XML like here or something else) – Dani Avni May 03 '13 at 09:36
  • @DaniAvni - I was hoping you could refactor your source code, so that the unit tests are simpler. However, if you need a smarter unit test, it would involve more work, but can be done using Reflection to iterate through all properties and assigning values based on the property's type. Hope this helps. – Channs May 03 '13 at 13:25