Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CollectionAssert.AreEqual失败。(索引0处的元素不匹配。)_C#_.net_Unit Testing_Collections_Nunit - Fatal编程技术网

C# CollectionAssert.AreEqual失败。(索引0处的元素不匹配。)

C# CollectionAssert.AreEqual失败。(索引0处的元素不匹配。),c#,.net,unit-testing,collections,nunit,C#,.net,Unit Testing,Collections,Nunit,由于实际值和预期值在索引0处返回相同的值,并且具有完全相同的属性,因此我似乎无法找到出现此错误的原因。这个问题的可能原因是什么?我环顾了四周,但到目前为止还找不到任何可行的解决办法 [TestMethod()] public void unSortedLeadsTest() { List<CustomerLead> expected = new List<CustomerLead>(); Li

由于
实际值
预期值
在索引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.");
        }
我怀疑:

foreach (CustomerLead i in target.addressList) {
    expected.Add(lead);
}
应该是:

foreach (CustomerLead i in target.addressList) {
    expected.Add(i);
}
否则,您将添加相同的引用三次

我不太清楚你想测试什么,请注意。。。您可能会对以下内容感到满意:

List<CustomerLead> expected = target.addressList.ToList();

编辑:此外,对于仅因为具有相同属性而被视为相等的两个对象,您需要重写
object.Equals(object)
,并理想地实现
IEquatable
。默认情况下,您只需要获得引用相等-任何两个不同的对象都被视为不相等,即使每个属性都相等。

首先,正如其他人所指出的,您必须正确实现
Equals
方法

public override bool Equals(对象对象对象)
{
if(obj==null)
返回false;
CustomerLead other=作为CustomerLead的obj;
if((对象)other==null)
返回false;
//这里需要比较两个对象
//下面只是一个示例实现
返回this.FirstName==other.FirstName
&&this.LastName==other.LastName
&&this.EmailAddress==其他.EmailAddress;
}

其次,在您的测试方法中,您不能使用正在测试的方法的结果值来准备预期的集合。如果
unSortedLeads
方法有简单错误,并将
FirstName
LastName
交换,则在该测试中永远不会发现此类错误。相反,您应该使用文字值

[TestMethod()]
public void unsortedledAdsTest()
{
//从xml读取对象
string xml=“C:/Users/Admin/Downloads/potentialcustomers.xml”;
CustomerReads target=新CustomerReads();
List actual=target.unSortedLeads(xml);
//准备预期的集合
预期列表=新列表()
{
新CustomerLead()
{
FirstName=“FirstName1”,
LastName=“LastName1”,
电子邮件地址=”Email@Address1"
},
新CustomerLead()
{
FirstName=“FirstName2”,
LastName=“LastName2”,
电子邮件地址=”Email@Address2"
},
新CustomerLead()
{
FirstName=“FirstName3”,
LastName=“LastName3”,
电子邮件地址=”Email@Address3"
}
};
//检验等式
收款资产等于(预期、实际);
}

您可以阅读有关实现
Equals
方法和单元测试的更多信息 { 新管理员用户{firstName=“test1”,lastName=“test1”,userId= “001test1”}, 新管理员用户{firstName=“test2”,lastName=“test2”,userId= “002test2”} }; //表演 List adminDetailsActual=RetrieveAdmin();//你的检索逻辑在这里 //断言 AreEqual(adminDetailsExpected.Count、adminDetailsActual.Count)//如果计数与else匹配,则测试成功。此计数可用作测试的变通方法
感谢您的快速回复。我已将foreach中的
lead
更改为
I
,但仍然得到相同的错误。另外,
.ToList
不会为
目标显示。地址列表
@user2708073:请参见我的编辑。至于为什么
ToList()
没有出现,您需要
使用System.Linq当然我们不知道
目标列表的类型,这没有帮助…谢谢,我该如何实现IEquatable?覆盖Equals(objecT)?@user2708073:那么,当覆盖
等于
时,您尝试了什么?你被什么卡住了?@user2708073:这不是关于集合,而是根据它们的属性使一个
CustomerLead
等同于另一个。阅读并查看此线程,以防其相关:
List<CustomerLead> expected = target.addressList.ToList();
using System.Linq;
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