CollectionAssert.AreEquivalent与Assert.Equals() [英] CollectionAssert.AreEquivalent vs Assert.Equals()
问题描述
public void NumericListCanBeSorted()
{
var sorted = sort.SortNumbers(nums);
Assert.AreEqual(sorted, nums);
}
public List<int> SortNumbers(List<int> nums)
{
List<int> sortedList = new List<int>();
for (int i = 0; i < nums.Count(); i++)
{
for (int j = i + 1; j < nums.Count; j++)
{
if (nums[i] > nums[j])
{
//Swapping values
nums[i] = nums[i] + nums[j];
nums[j] = nums[i] - nums[j];
nums[i] = nums[i] - nums[j];
}
}
sortedList.Add(nums[i]);
}
return sortedList;
}
Result Message: Assert.AreEqual failed.
Expected: <System.Collections.Generic.List`1[System.Int32]>.
Actual :<System.Collections.Generic.List`1[System.Int32]>.
期望和实际的数据类型相同。仍然测试失败。不明白为什么?有人可以帮我知道原因,此外,它适用于以下功能。
The datatypes of expected and actual are same. Still test failed. Cannot understand why? Can someone please help me know the reason.Moreover, it works for the following function.
public List<int> SortNumbers(List<int> nums)
{
List<int> sortedList = new List<int>();
for (int i = 0; i < nums.Count(); i++)
{
for (int j = i + 1; j < nums.Count; j++)
{
if (nums[i] > nums[j])
{
//Swapping values
nums[i] = nums[i] + nums[j];
nums[j] = nums[i] - nums[j];
nums[i] = nums[i] - nums[j];
}
}
}
sortedList = nums;
return sortedList;
}
推荐答案
引用类型比较。
尝试使用CollectionAssert.AreEqual或CollectionAssert.AreEquivalent。
Try using CollectionAssert.AreEqual or CollectionAssert.AreEquivalent.
发生的情况是
您使用 Assert.AreEqual(List< int< List< int>)
,其中,CLR / .NET会说:列表1的引用与列表相同吗? 2?结果为否,因此CLR / .NET向您发送错误的读数。
You use Assert.AreEqual(List<int>, List<int>)
, of which, the CLR/.NET go in and say, "is the reference of List 1 the same as List 2?" The result is no, therefore the CLR/.NET send you a false reading.
CollectionAssert
枚举数组(这就是列表内部的内容),并确定它们是否具有相同的项目。 CollectionAssert.AreEqual
检查它们是否具有相同顺序的相同项目。 CollectionAssert.AreEquivalent
检查它们是否具有相同顺序的项目。
CollectionAssert
enumerates the arrays (which is what Lists are internally) and determines if they have the same items. CollectionAssert.AreEqual
check that they have the same items in the same order. CollectionAssert.AreEquivalent
check that they have the same items, in any order.
ANY 数组/列表/字典必须使用 CollectionAssert
进行此类比较。
ANY array/List/Dictionary MUST use CollectionAssert
for such comparisons.
例如:
List<int> l1 = new List<int>();
List<int> l2 = l1;
Assert.AreEqual(l1, l2);
由于 l2
已设置为与 l1
相同的引用。
This will result in true, due to l2
having been set to the same reference as l1
.
但是,
List<int> l1 = new List<int>();
List<int> l2 = new List<int>();
Assert.AreEqual(l1, l2);
由于,这将导致 false l2
已成为对 NEW 对象的 NEW 引用。而使用
This will result in a false due to l2
having been made a NEW reference to a NEW object. Whereas using
CollectionAssert.AreEqual(l1, l2);
在上述两种情况下均会导致 true 。这是因为CLR / .NET实际上实际上是遍历该列表并进行清点。
Will result in a true in either situation above. This is because the CLR/.NET actually go through the list and inventory it, essentially.
另一种编辑,因为我想进一步说明:您也可以使用 CollectionAssert.AreEquivalent
,它不能保证项目具有相同的顺序,而只是保证数组/列表/字典具有相同数量的相同项目。即:
Another edit, because I wanted to clarify further: you can also use CollectionAssert.AreEquivalent
, which will not guarantee the items in the same order, but will simply guarantee that the arrays/Lists/Dictionaries have the same quantities of the same items. I.e.:
1,2,3,3,2,1
1,3,2,2,3,1
这将导致 true 和 CollectionAssert.AreEquivalent
,但 false 与 CollectionAssert.AreEqual
一样。
This will result in true with CollectionAssert.AreEquivalent
, but false with CollectionAssert.AreEqual
.
这篇关于CollectionAssert.AreEquivalent与Assert.Equals()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!