public class Student
{
public string Name { get; set; }
public int ID { get; set; }
}
...
var st1 = new Student
{
ID =
What you are looking for is what in xUnit Test Patterns is called Test-Specific Equality.
While you can sometimes choose to override the Equals method, this may lead to Equality Pollution because the implementation you need to the test may not be the correct one for the type in general.
For example, Domain-Driven Design distinguishes between Entities and Value Objects, and those have vastly different equality semantics.
When this is the case, you can write a custom comparison for the type in question.
If you get tired doing this, AutoFixture's Likeness class offers general-purpose Test-Specific Equality. With your Student class, this would allow you to write a test like this:
[TestMethod]
public void VerifyThatStudentAreEqual()
{
Student st1 = new Student();
st1.ID = 20;
st1.Name = "ligaoren";
Student st2 = new Student();
st2.ID = 20;
st2.Name = "ligaoren";
var expectedStudent = new Likeness<Student, Student>(st1);
Assert.AreEqual(expectedStudent, st2);
}
This doesn't require you to override Equals on Student.
Likeness performs a semantic comparison, so it can also compare two different types as long as they are semantically similar.
It looked liked AutoFixture's Likeness is what I needed for this problem (thanks Mark Seeman) however it does not support comparing collection elements for likeness (there's a couple open issues on the matter but they have not been resolved).
I found CompareObjects by Kellerman Software does the trick:
https://github.com/GregFinzer/Compare-Net-Objects
http://www.infoq.com/articles/Equality-Overloading-DotNET
This article may be usefull , I solve this problem just using refcetion dump all filed out; Then we just need compare two strings.
Code Here:
/// <summary>
/// output all properties and values of obj
/// </summary>
/// <param name="obj"></param>
/// <param name="separator">default as ";"</param>
/// <returns>properties and values of obj,with specified separator </returns>
/// <Author>ligaoren</Author>
public static string Dump(object obj, string separator)
{
try
{
if (obj == null)
{
return string.Empty;
}
if (string.IsNullOrEmpty(separator))
{
separator = ";";
}
Type t = obj.GetType();
StringBuilder info = new StringBuilder(t.Name).Append(" Values : ");
foreach (PropertyInfo item in t.GetProperties())
{
object value = t.GetProperty(item.Name).GetValue(obj, null);
info.AppendFormat("[{0}:{1}]{2}", item.Name, value, separator);
}
return info.ToString();
}
catch (Exception ex)
{
log.Error("Dump Exception", ex);
return string.Empty;
}
}
I just did:
Assert.AreEqual(Newtonsoft.Json.JsonConvert.SerializeObject(object1),
Newtonsoft.Json.JsonConvert.SerializeObject(object2));
You can also use NFluent with the this syntax to deep compare two objects without implementing equality for your objects. NFluent is a library that tries to simplify writing readable test code.
Check.That(actual).HasFieldsWithSameValues(expected);
This method with fail with an exception containing all the differences instead of failing at the first one. I find this feature to be a plus.
If comparing public members is enough for your use-case, simply jam your objects into JSON and compare the resulting strings:
var js = new JavaScriptSerializer();
Assert.AreEqual(js.Serialize(st1), js.Serialize(st2));
JavaScriptSerializer Class
Pros
Equals
Cons