Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between EqualTo() and EquivalentTo() in NUnit?

When I have a Dictionary<string, int> actual and then create a completely new Dictionary<string, int> expected with the same values as actual.

  • Calling Assert.That(actual, Is.EqualTo(expected)); makes the test pass.

  • When using Assert.That(actual, Is.EquivalentTo(expected)); the test doesn't pass.

What is the difference between EqualTo() and EquivalentTo()?

Edit:

The message of the exception when the test doesn't pass is as follows:

Zoozle.Tests.Unit.PredictionTests.ReturnsDriversSelectedMoreThanOnceAndTheirPositions:
Expected: equivalent to < [Michael Schumacher, System.Collections.Generic.List`1[System.Int32]] >
But was:  < [Michael Schumacher, System.Collections.Generic.List`1[System.Int32]] >

My code looks like this:

[Test]
public void ReturnsDriversSelectedMoreThanOnceAndTheirPositions()
{
    //arrange
    Prediction prediction = new Prediction();

    Dictionary<string, List<int>> expected = new Dictionary<string, List<int>>()
    {
        { "Michael Schumacher", new List<int> { 1, 2 } }
    };

    //act
    var actual = prediction.CheckForDriversSelectedMoreThanOnce();

    //assert
    //Assert.That(actual, Is.EqualTo(expected));
    Assert.That(actual, Is.EquivalentTo(expected));
}

public Dictionary<string, List<int>> CheckForDriversSelectedMoreThanOnce()
{
    Dictionary<string, List<int>> expected = new Dictionary<string, List<int>>();
    expected.Add("Michael Schumacher", new List<int> { 1, 2 });

    return expected;
}
like image 744
Garth Marenghi Avatar asked Jun 29 '11 08:06

Garth Marenghi


People also ask

How do I compare two objects in NUnit?

Just install ExpectedObjects from Nuget, you can easily compare two objects's property value, each object value of collection, two composed object's value and partial compare property value by anonymous type. Reference: ExpectedObjects github.

What is the correct syntax to check whether the expected and actual value are equal in NUnit?

An EqualConstraint is used to test whether an actual value is equal to the expected value supplied in its constructor, optionally within a specified tolerance.


1 Answers

The question title forces me to state the following:

For enumerations, Is.EquivalentTo() does the comparison allowing any order of the elements. In contrast, Is.EqualTo() takes into account the exact order of the elements, like Enumerable.SequenceEqual() does.

However, in your case, there is no issue with ordering. The main point here is that Is.EqualTo() has extra code for dictionary comparison, as stated here.

Not so Is.EquivalentTo(). In your example, it will compare values of type KeyValuePair<string,List<int>> for equality, using object.Equals(). Since the dictionary values are of reference type List<int>, reference equality is used for comparing them.

If you modify your example such that the List {1, 2} is only instantiated once and used in both dictionaries, Is.EquivalentTo() will succeed.

like image 186
tm1 Avatar answered Sep 23 '22 12:09

tm1