I'd like to know whether there is a way to compare two objects in MBUnit so that the test is passed when the objects "look" the same, even if those are distinct instances?
For example:
[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 (test fails, see source code above). Since it remarks that "Both values look the same when formatted but they are distinct instances", I figure there must be some way to do this built into MbUnit already without serializing the objects to XML in my own code.
Do I have to write my own Assert extension method for this?
Yann also implemented a StructuralEqualityComparer that compares property values one by one given a set of lambdas for each property. Worth looking at.
More info here: http://www.gallio.org/api/html/T_MbUnit_Framework_StructuralEqualityComparer_1.htm
There is an overload of Assert.AreEqual()
that takes an IEqualityComparer<T>
as parameter and another that takes a EqualityComparison<T>
Otherwise you could use Assert.AreEqual(Assert.XmlSerialize(a), Assert.XmlSerialize(b))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With