3

我似乎无法找出为什么我会收到此错误,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);
        }
4

3 回答 3

9

我怀疑这是:

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>。默认情况下,您只会获得引用相等 - 即使每个属性都相等,任何两个不同的对象都被认为是不相等的。

于 2013-11-06T15:23:06.660 回答
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交换,则此测试将永远不会发现此类错误。相反,您应该使用文字值。FirstNameLastName

[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);
}


您可以在此处阅读有关实施Equals方法的更多信息,并在此处阅读有关单元测试的更多信息

于 2013-11-27T15:04:43.107 回答
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
于 2019-12-20T12:24:49.287 回答