chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Using this custom checker to validate that my events have the correct property values:

///-------------------------------------------------------------------------------------
/// <summary>
/// Determine if the properties of two objects are equivelent.
/// </summary>
/// <remarks>Note that you don't need to use this if the argument you are validating
/// has an overloaded Equal() method (like string, etc...)</remarks>
/// <param name="args">Arguments</param>
/// <returns>true if Equivelent, false otherwise.</returns>
///-------------------------------------------------------------------------------------
public static bool PropertiesEqualChecker( ParameterCheckerEventArgs args )
{
   // Enumerate the properties of the expected object and verify the arguments match
   if ( args.ExpectedValue.GetType() != args.ArgumentValue.GetType() )
      return false;

   foreach ( PropertyInfo pi in args.ExpectedValue.GetType().GetProperties() )
   {
      // You would think there would be an easier way to do this!
      string expected = pi.GetValue( args.ExpectedValue, null ).ToString();
      string argument = pi.GetValue( args.ArgumentValue, null ).ToString();

      if ( expected != argument )
         return false;
   }

   return true;
}
asked by DaveL (1.7k points)

2 Answers

0 votes
Hi,

Thanks for sharing this.
This seems like a sound feature.
How often would you use this check?

Of course we should add a check for null ExpectedValue and ArgumentValue.
and do a real check against the values of the Parameters and not its string representation.
answered by scott (32k points)
0 votes
I would use this for any object that doesn't override Equals() that is created internal to the class under test.

This allows me to say that the output (event) should have these exact properties.

BTW, I did the string comparison since even boxed integers with the same value are not "Equal". I'm not sure how to unbox and compare without knowing the original type.
answered by DaveL (1.7k points)
...