sách gpt4 ăn đã đi

c# - 无法比较单元测试中的列表

In lại 作者:太空狗 更新时间:2023-10-30 01:20:14 27 4
mua khóa gpt4 giày nike

我需要在我的单元测试中比较如下列表:

var x = new List() { new List() };
var y = new List() { new List() };
CollectionAssert.AreEqual(x, y, "Expected response not the same as actual response.");

但我总是遇到以下异常,我该如何克服这个问题?

[Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException] = {"CollectionAssert.AreEqual failed. Expected response not the same as actual response.(Element at index 0 do not match.)"}

câu trả lời hay nhất

根据 msdn 文档。 http://msdn.microsoft.com/en-us/library/ms243736.aspx

Two collections are equal if they have the same elements in the same order and quantity. Elements are equal if their values are equal, not if they refer to the same object. The values of elements are compared using Equals by default.

现在看来集合是相等的。直到你更深入地观察。根据文档

have the same elements in the same order and quantity

从您的示例来看,它们没有相同的元素。它们具有相同类型的元素,并且这些元素具有相似的签名,但是这两个元素并不相同。它们是完全不同的对象。

使用“相同顺序的相同元素”运行测试并查看结果。比如。

List list = new List();
var x = new List() { list };
var y = new List() { list };
CollectionAssert.AreEqual(x, y, "Expected response not the same as actual response.");

您会发现这会随着满足 CollectionAssert.AreEqual 的参数列表而传递。

希望这能解决问题。

关于c# - 无法比较单元测试中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19724524/

27 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress