Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test two objects are equal in xunit.net?

I am reading Test Driven Development: By Example. All examples use Java and Junit (I am on chapter 10). There are one test method that test for equality of two objects. I already override Equals of the class but when run my test it failed.

This is sample code

public class BaseX
{
    public string Test { get; set; }

    public override bool Equals(object obj)
    {
        return this.Test == ((BaseX)obj).Test;
    }

    public override string ToString()
    {
        return string.Format("Tyep: {0}, Test: {1}", this.GetType().Name, this.Test);
    }
}

public class A : BaseX
{

}

This is my test code

[Fact]
public void FunTest2()
{
    var b1 = new BaseX();
    var a1 = new A();

    b1.Test = "a";
    a1.Test = "a";

    Assert.Equal(a1, b1);
}

When I run the test, it will failed with this message.

TDD1.UnitTest.UnitTest1.FunTest2 : Assert.Equal() Failure
Expected: Tyep: A, Test: a
Actual:   Tyep: BaseX, Test: a

I think Assert.Equal compare both value and type of objects. So, I looked on xunit code and found that Assert.Equal call IEqualityComparer.Equals. If I want to compare two object with override method, what method should I use?

Update
I test this on Windows 7, Visual Studio 11 Beta, xunit.net 1.9.0.1566 (get files from nuget)

like image 426
Anonymous Avatar asked Apr 28 '12 17:04

Anonymous


People also ask

How do you assert two objects are equal in xUnit?

You'll have to implement IEquatable<T> for your objects, and then Assert. Equals will work. Assert. Same() compares by reference; it asserts that Obj1 and Obj2 are the same object rather than just looking the same.

How do you compare two objects in assert?

The assert. deepEqual() method tests if two objects, and their child objects, are equal, using the == operator. If the two objects are not equal, an assertion failure is being caused, and the program is terminated. To compare the objects using the === operator, use the assert.

How do you assert equal in C#?

AreEqual(Object, Object, String)Tests whether the specified objects are equal and throws an exception if the two objects are not equal. Different numeric types are treated as unequal even if the logical values are equal. 42L is not equal to 42.


1 Answers

Before comparing both objects using T's Equals method, xunit compares types:

// Same type?
if (!skipTypeCheck && x.GetType() != y.GetType())
    return false;

As I see it, you have two choices:

The simple choice

Assert.True(b1.Equals(a1));

It might be less expected than an Equal overload, but KISS...

The less simple choice

public class BaseXComparer : IEqualityComparer<BaseX>
{
    public bool Equals(BaseX x, BaseX y)
    {
        return x.Test.Equals(y.Test);
    }

    public int GetHashCode(BaseX obj)
    {
        return obj.Test.GetHashCode();
    }
}

And then:

Assert.Equal(a1, b1, new BaseXComparer());

In this case, consider this.

Until someone will add a new overload (shouldn't be tricky, as the inner implementation has a bool parameter for this) or an extension, I'd recommend using the simple method above.

like image 132
seldary Avatar answered Sep 23 '22 15:09

seldary