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

标签 c# .net unit-testing collections nunit

我似乎无法找出为什么我会收到此错误,因为 actualexpected 在索引 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/

相关文章:

c# - Windows 服务应该构建为 Windows 应用程序还是控制台应用程序?

C# 泛型 : there is no way to constrain a type to have a static method?

c# - 可用页面文件大小/虚拟内存

c# - C# (.NET) 中是否有更好的 Web 浏览器控件?

c# - 使用 C# 将字符串转换为 SqlServer 的日期时间

c# - 使用 C# 和存储过程从 SQL Server 检索 VarChar(MAX)

c# - 我如何并行化这种模式?

c# - Entity Framework 。测试 SaveChanges 是否存在并在方法中的正确位置调用

java - 在没有源代码的情况下使用 Espresso for APK 编写 UI 测试

java - 查看单个类的代码覆盖率