use*_*073 2 .net c# collections nunit unit-testing
我似乎无法找出为什么我得到这个错误,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.");
}
Run Code Online (Sandbox Code Playgroud)
编辑:香港专业教育学院试图超越平等,但我正在努力:任何想法我怎么可能实现这一点?
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);
}
Run Code Online (Sandbox Code Playgroud)
我怀疑这个:
foreach (CustomerLead i in target.addressList) {
expected.Add(lead);
}
Run Code Online (Sandbox Code Playgroud)
应该:
foreach (CustomerLead i in target.addressList) {
expected.Add(i);
}
Run Code Online (Sandbox Code Playgroud)
否则,您将添加相同的引用三次.
我不太清楚你要测试的是什么,请注意......你可能会很好:
List<CustomerLead> expected = target.addressList.ToList();
Run Code Online (Sandbox Code Playgroud)
......以及以下using
指令:
using System.Linq;
Run Code Online (Sandbox Code Playgroud)
编辑:此外,为了使两个对象被认为是相同的,因为它们具有相同的属性,您需要覆盖object.Equals(object)
并理想地实现IEquatable<CustomerLead>
.默认情况下,您只需获得引用相等性 - 即使每个属性相等,任何两个不同的对象都被视为不相等.
首先,正如其他人所指出的那样,你必须正确地实现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;
}
Run Code Online (Sandbox Code Playgroud)
其次,在您的测试方法中,您不得使用您正在测试的方法的结果值来准备预期的集合.如果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);
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处阅读有关实施Equals
方法和单元测试的更多信息