Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MbUnit: Comparing distinct object instances

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?

like image 974
Adrian Grigore Avatar asked Dec 14 '22 02:12

Adrian Grigore


2 Answers

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

like image 184
Jeff Brown Avatar answered Dec 16 '22 16:12

Jeff Brown


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))

like image 38
Mauricio Scheffer Avatar answered Dec 16 '22 18:12

Mauricio Scheffer