我似乎无法找出为什么我收到此错误,因为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);
}
答案 0 :(得分:9)
我怀疑这个:
foreach (CustomerLead i in target.addressList) {
expected.Add(lead);
}
应该是:
foreach (CustomerLead i in target.addressList) {
expected.Add(i);
}
否则你将三次添加相同的引用。
我不太清楚你要测试的是什么,请注意......你可能会很好:
List<CustomerLead> expected = target.addressList.ToList();
...以及using
指令:
using System.Linq;
编辑:此外,由于两个对象具有相同的属性而被认为是相同的,因此您需要覆盖object.Equals(object)
并理想地实现IEquatable<CustomerLead>
。默认情况下,您只需获得引用相等性 - 任何两个不同的对象都被视为不相等,即使每个属性都相等。
答案 1 :(得分:5)
首先,正如其他人指出的那样,您必须正确实施Equals
方法。
public override bool Equals(Object obj)
{
if (obj == null)
return false;
CustomerLead other = obj as CustomerLead;
if ((Object)other == null)
return false;
// here you need to compare two objects
// below is just example implementation
return this.FirstName == other.FirstName
&& this.LastName == other.LastName
&& this.EmailAddress == other.EmailAddress;
}
其次,在您的测试方法中,您不得使用您正在测试的方法的结果值来准备预期的集合。如果unSortedLeads
方法存在简单错误并将FirstName
与LastName
交换,则您将永远不会在此测试中发现此类错误。相反,你应该使用文字值。
[TestMethod()]
public void unSortedLeadsTest()
{
// read objects from xml
string xml = "C:/Users/Admin/Downloads/potentialcustomers.xml";
CustomerLeads target = new CustomerLeads();
List<CustomerLead> actual = target.unSortedLeads(xml);
// prepare expected collection
List<CustomerLead> expected = new List<CustomerLead>()
{
new CustomerLead()
{
FirstName = "FirstName1",
LastName = "LastName1",
EmailAddress = "Email@Address1"
},
new CustomerLead()
{
FirstName = "FirstName2",
LastName = "LastName2",
EmailAddress = "Email@Address2"
},
new CustomerLead()
{
FirstName = "FirstName3",
LastName = "LastName3",
EmailAddress = "Email@Address3"
}
};
// test equality
CollectionAssert.AreEqual(expected, actual);
}
答案 2 :(得分:0)
List<AdminUser> adminDetailsExpected = new List<AdminUser>()
{
new AdminUser {firstName = "test1" , lastName = "test1" , userId =
"001test1" },
new AdminUser {firstName = "test2" , lastName = "test2" , userId =
"002test2" }
};
//Act
List<AdminUser> adminDetailsActual = RetrieveAdmin(); // your retrieve logic goes here
//Assert
Assert.AreEqual(adminDetailsExpected.Count, adminDetailsActual.Count); //Test succeeds if the count matches else fails. This count can be used as a work around to test