我试图为传递一个AllNumbers的int []数组的方法创建一个测试,并返回一个仅包含EvenNumbers的int []数组。虽然调试测试显示Expected
和Actual
相同,但测试仍然失败。我猜测它是.Equals
与==
问题?
错误:
Failed TestEvenNumbers CalculatorEngineTests Assert.AreEqual failed. Expected:<System.Int32[]>. Actual:<System.Int32[]>.
这是我的测试:
[TestMethod]
public void TestEvenNumbers()
{
Calculator target = new Calculator();
int[] test = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] expected = { 2, 4, 6, 8 };
int[] actual = target.GetEvenNumbers(test);
//This passes
Assert.AreEqual(expected[1], actual[1]);
//This fails
Assert.AreEqual(expected, actual);
}
这是我要测试的方法:
public int[] GetEvenNumbers(int[] arr)
{
var evenNums =
from num in arr
where num % 2 == 0
select num;
return evenNums.ToArray<int>();
}
答案 0 :(得分:3)
尝试:
CollectionAssert.AreEqual(expected, actual);
断言集合包含相同的东西,我相信Assert.AreEqual将简单地比较引用。