在多维数组上使用CollectionAssert.AreEquivalent()时出现RankException.<



我正在比较两个多维数组是否相等,当我发现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修复了,应该在下一个版本中可用。

相关内容

最新更新