我似乎无法找出为什么我会收到此错误,因为 actual
和 expected
在索引 0 处返回相同的值并且具有完全相同的属性。这个问题的可能原因是什么?我环顾四周,但至今找不到任何可行的解决方案。
[TestMethod()]
public void unSortedLeadsTest()
{
List<CustomerLead> expected = new List<CustomerLead>();
List<CustomerLead> actual = new List<CustomerLead>();
CustomerLeads target = new CustomerLeads(); // TODO: Initialize to an appropriate value
string xml = "C:/Users/Admin/Downloads/potentialcustomers.xml"; // TODO: Initialize to an appropriate value
actual = target.unSortedLeads(xml);
CustomerLead lead = new CustomerLead()
{
FirstName = actual[0].FirstName,
LastName=actual[0].LastName,
EmailAddress=actual[0].EmailAddress
};
CustomerLead lead1 = new CustomerLead()
{
FirstName = actual[1].FirstName,
LastName = actual[1].LastName,
EmailAddress = actual[1].EmailAddress
};
CustomerLead lead2 = new CustomerLead()
{
FirstName = actual[2].FirstName,
LastName = actual[2].LastName,
EmailAddress = actual[2].EmailAddress
};
target.addressList.Add(lead);
target.addressList.Add(lead1);
target.addressList.Add(lead2);
foreach (CustomerLead i in target.addressList) {
expected.Add(lead);
}
// TODO: Initialize to an appropriate value
CollectionAssert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}
编辑:我试图覆盖 Equals 但我很挣扎:有什么想法可以实现吗?
public override bool Equals(Object obj)
{
if (obj == null)
return false;
CustomerLead leadsequal = obj as CustomerLead;
if ((Object)leadsequal == null)
return false;
else
return Equals( leadsequal);
}
最佳答案
我怀疑是这样的:
foreach (CustomerLead i in target.addressList) {
expected.Add(lead);
}
应该是:
foreach (CustomerLead i in target.addressList) {
expected.Add(i);
}
否则您将添加相同的引用 3 次。
我不太清楚你要测试什么,请注意......你可能只需要:
List<CustomerLead> expected = target.addressList.ToList();
... 以及 using
指令:
using System.Linq;
编辑:另外,如果两个对象仅仅因为它们具有相同的属性就被认为是相等的,您需要覆盖 object.Equals(object)
并理想地实现 IEquatable<CustomerLead>
也。默认情况下,您只会获得引用相等性 - 任何两个不同的对象都被视为不相等,即使每个属性都相等。
关于c# - CollectionAssert.AreEqual 失败。 (索引 0 处的元素不匹配。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19815873/