Search code examples
c#.net-corenunit

RankException when using CollectionAssert.AreEquivalent() on Multi-dimensional Arrays


I was comparing two Multi-dimensional Arrays for equality like so, when I discovered that AreEquivalent throws a RankException for any Multi-dimensional Array:

object[,] expected = new object[,] { { 
    new Dictionary<string, string> { { "", "" } } 
} };
var actual = expected;

CollectionAssert.AreEquivalent(expected, actual);   // throws RankException

Exception message:

System.RankException : The specified arrays must have the same number of dimensions.

We can see similar behaviour for a simpler multi-dimensional array:

string[,] expected = new string[,] { { "value1", "value2" } };
var actual = expected;

CollectionAssert.AreEquivalent(expected, actual);   // throws RankException

I can get the expected result by using AreEqual instead of AreEquivalent. Like so:

string[,] expected = new string[,] { { "value1", "value2" } };
var actual = expected;

CollectionAssert.AreEqual(expected, actual);   // does not throw Exception

But I don't understand why. Isn't AreEqual supposed to be a subset of AreEquivalent?

If the only difference between these two methods is that AreEqual checks the order and AreEquivalent doesn't, then AreEquivalent should also evaluate to true.

Is this a bug or am I misunderstanding some detail about CollectionAssert?


Solution

  • This seems that this is a bug in NUnit. Somewhere in the implementation ArrayList's constructor accepting ICollection is used which fails with the aforementioned exception:

    var throws = new ArrayList(actual);
    

    Submitted NUnit bug (and runtime one).

    UPD

    Issue was already fixed by this PR and should be available with the next release.