Lets say you want to do some TDD, as you may know that you have in some/most test an Expected answer and an Actual answer. In the case of the expected/actual being a value type or string its easy just to use:
Assert.AreEqual<string> ("this is expected", Actual, "the string values did not match");
However in the case where these varibles are business objects, you will need to override the "==" and/or the Equals operators. Thats perfect when all you business objects have this implemented, but what can you do when they don't. One thing that comes to mind is either going and adding overrided methods in all your object, or you could add an assert for every property in the class. Well I was a little bit lazy/curious to see if I could use reflection, and this is what I came up with:
public static bool isEqual(object Actual, object Expected)
{
if (Actual.GetType() != Expected.GetType())
{
throw new ArgumentException
("both inputs have to be of the same type");
}
Type t = Actual.GetType();
PropertyInfo[] ps = t.GetProperties();
foreach (PropertyInfo p in ps)
{
//only test the value types and strings
if ((p.PropertyType.IsValueType) ||
(p.PropertyType == typeof(string)))
{
List<Type> paramTypes = new List<Type>();
string PropertyName = p.Name;
//not needed as they should not have parameters
//foreach (ParameterInfo param in p.GetIndexParameters())
//{
// paramTypes.Add(param.ParameterType);
//}
//get the first value
object val1 = t.GetProperty(PropertyName,
paramTypes.ToArray()).GetValue(Actual,
null);
//get the second value
object val2 = t.GetProperty(PropertyName,
paramTypes.ToArray()).GetValue(Expected,
null);
//make sure they are the same
if (!val1.ToString().Equals(val2.ToString()))
{
return false;
}
}
}
return true;
}
The above uses reflection to test all the value type and string properties in the object to see if they are the same. This will take in 2 objects of the same type and return true if they contain the same values (a little like the assert.AreEqual<youObject>).
Take the following example:
static void Main(string[] args)
{
person p1 = new person();
person p2 = new person();
person p3 = new person();
person p4 = p1;
p1.Name = "dave";
p1.Age = 25;
p2.Name = "dave";
p2.Age = 25;
p3.Age = 23;
p3.Name = "troy";
Console.WriteLine(string.Format("p1 = p2 : {0}", p1 == p2));
Console.WriteLine(string.Format("p1 = p4 : {0}", p1 == p4));
Console.WriteLine(string.Format("isEqual(p1, p2) : {0}", isEqual(p1, p2)));
Console.WriteLine(string.Format("isEqual(p3, p2) : {0}", isEqual(p3, p2)));
Console.ReadLine();
}
note the isEqual(p1, p2) is true, which is awesome for some cases of unit testing. (consider the saving and loading of an object to and from a DB, testing to see if it returned the same object)
finally the code for the person class: (as you can see its a simple class)
class person
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
}
but if you would like to know how to override the "==" and Equals take a look at this MSDN article:
http://msdn2.microsoft.com/en-us/library/ms173147.aspx
Currently rated 3.0 by 1 people
- Currently 3/5 Stars.
- 1
- 2
- 3
- 4
- 5