我正在比较两个多维数组是否相等,当我发现AreEquivalent
为任何多维数组抛出RankException
时:
object[,] expected = new object[,] { {
new Dictionary<string, string> { { "", "" } }
} };
var actual = expected;
CollectionAssert.AreEquivalent(expected, actual); // throws RankException
异常信息:
System.RankException : The specified arrays must have the same number of dimensions.
对于一个更简单的多维数组,我们可以看到类似的行为:
string[,] expected = new string[,] { { "value1", "value2" } };
var actual = expected;
CollectionAssert.AreEquivalent(expected, actual); // throws RankException
用AreEqual
代替AreEquivalent
可以得到预期的结果。像这样:
string[,] expected = new string[,] { { "value1", "value2" } };
var actual = expected;
CollectionAssert.AreEqual(expected, actual); // does not throw Exception
但是我不明白为什么。AreEqual
不应该是AreEquivalent
的子集吗?
如果这两个方法之间的唯一区别是AreEqual
检查顺序而AreEquivalent
不检查,那么AreEquivalent
也应该计算为真。
这是一个bug还是我误解了CollectionAssert
的一些细节?
这似乎是NUnit中的一个bug。在实现的某个地方,使用了ArrayList
的构造函数接受ICollection
,但由于上述异常而失败:
var throws = new ArrayList(actual);
提交了NUnit bug(和运行时bug)。
乌利希期刊指南
这个问题已经被这个PR修复了,应该在下一个版本中可用。