MbUnit: comparing individual instances of objects

I would like to know if there is a way to compare two objects in MBUnit so that the test is passed when the objects "look" the same, even if they are different instances?

For instance:

[TestFixture] class ComparisonTestFixture { class foo { public string bar; } [Test] public void ComparisonTest() { foo foo1 = new foo() { bar = "baz" }; foo foo2 = new foo() { bar = "baz" }; //This assertion should be successful, but it isn't //*** Failures *** //Expected values to be equal. //Expected Value & Actual Value : {foo: bar = "zzz...."} //Remark : Both values look the same when formatted but they are distinct instances. Assert.AreEqual(foo1,foo2); } } 

Assert.AreEqual () does not work for this (the test fails, see the source code above). Since he notices that β€œBoth values ​​look the same when formatted, but they are different instances,” I believe there should be some way to do this built in to MbUnit without serializing the objects in XML in my own code.

Should I write my own Assert extension method for this?

+4
source share
4 answers

I would advise you to override the Equals method for your class to make the required comparison. This allows you to define equality of values ​​instead of referential equality. One caveat is that you should also override GetHashCode if you override Equals to ensure that two identical objects return the same hash code. Here is a very simple example:

 public class Foo { public String Bar { get; set; } public String Baz { get; set; } public override Boolean Equals(Object other) { Foo otherFoo = other as Foo; return otherFoo != null && Bar.Equals(otherFoo.Bar) && Baz.Equals(otherFoo.Baz); } public override Int32 GetHashCode() { return Bar.GetHashCode() ^ Baz.GetHasCode(); } } 

If you don't want to override Equals , and you really just want to compare instance properties by property, you can use reflection:

 public static Boolean AreEqual<T>(T a, T b) { foreach (PropertyInfo propertyInfo in typeof(T).GetProperties()) if (!Object.Equals(propertyInfo.GetValue(a, null), propertyInfo.GetValue(b, null))) return false; return true; } 
+1
source

Yann also implemented a StructuralEqualityComparer, which compares property values ​​one by one, taking into account the set of lambdas for each property. Worth a look.

More information here: http://www.gallio.org/api/html/T_MbUnit_Framework_StructuralEqualityComparer_1.htm

+7
source

There is an overload of Assert.AreEqual() that accepts the IEqualityComparer<T> as parameter and the other EqualityComparison<T>

Otherwise, you can use Assert.AreEqual(Assert.XmlSerialize(a), Assert.XmlSerialize(b))

+4
source

What I usually do is simply implement the ToString () override, which is considered the best option anyway.

so in your case:

 public override string ToString() { return string.Format("Class foo, bar={0}",bar); } 

then your AreEqual(foo1,foo2) will report the correct results, as it will just call the default ToString implementation

+1
source

All Articles