C#将列表与自定义对象进行比较但忽略顺序

时间:2013-09-05 09:02:09

标签: c# collections comparison nunit assert

我正在尝试比较包含自定义对象的2个列表(包裹在对象中)。 我不关心订单,但如果列表1包含“1,2,3,4”,则列表2必须且仅包含这些元素。例如:“4,2,3,1”

基于Compare two List<T> objects for equality, ignoring order 忽略顺序我使用了Except和Any但它没有给我预期的结果。

如果我使用Assert.Equals,则会失败,但Assert.IsTry(list1.equals(list2))会成功。

如果我删除Equals和GetHashCode实现,那么两个测试都会失败。

public class AppointmentCollection : List<Appointment>
{
    public override bool Equals(object obj)
    {            
        var appCol = obj as AppointmentCollection;

        if (appCol == null)
        {
            return false;
        }

        return (appCol.Count == this.Count) && !(this.Except(appCol).Any());
    }


    public override int GetHashCode()
    {
        unchecked
        {
            //use 2 primes
            int hash = 17;
            foreach (var appointment in this)
            {
                hash = hash * 19 + appointment.GetHashCode();
            }
            return hash;
        }
    }
}

public class Appointment
{
    public string Title {get; set;}
    public DateTime StartTime {get; set;}
    public DateTime EndTime { get; set;}

    public override bool Equals(object obj)
    {
        var appointment = obj as Appointment;
        if (appointment == null)
        {
            return false;
        }
        return Title.Equals(appointment.Title) &&
            StartTime.Equals(appointment.StartTime) &&
            EndTime.Equals(appointment.EndTime);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            //use 2 primes
            int hash = 17;
            hash = hash * 19 + Title.GetHashCode();
            hash = hash * 19 + StartTime.GetHashCode();
            hash = hash * 19 + EndTime.GetHashCode();
            return hash;
        }
    }
}

[Test]
public void TestAppointmentListComparisonDifferentOrder()
{
    var appointment1 = new Appointment(
        "equals test1",
        new DateTime(2013, 9, 4),
        new DateTime(2013, 9, 4));

    var appointment2 = new Appointment(
        "equals test2",
        new DateTime(2013, 9, 4),
        new DateTime(2013, 9, 4));

    var list1 = new AppointmentCollection() { appointment1, appointment2 };
    var list2 = new AppointmentCollection() { appointment2, appointment1 };

    //With Equals/GetHashCode in AppointmentCollection implemented
    CollectionAssert.AreEqual(list1, list2); //fails
    Assert.IsTrue(list1.Equals(list2)); //success

    //Without Equals/GetHashCode in AppointmentCollection implemented
    CollectionAssert.AreEqual(list1, list2); //fails
    Assert.IsTrue(list1.Equals(list2)); //fails
}

1 个答案:

答案 0 :(得分:3)

您没有明确说明您使用的是哪种单元测试工具。也许CollectionAssertMicrosoft.VisualStudio.TestTools.UnitTesting.CollectionAssert类,或者可能是NUnit.Framework.CollectionAssert,或者其他可能是什么?

请查看测试工具的文档,或在此处写下您使用的文档。

然而,

很常见
CollectionAssert.AreEqual( ... );

检查集合是否以相同的顺序相同,而

CollectionAssert.AreEquivalent( ... );

会检查你想要的是什么。所以使用后者。

CollectionAssert上的两种方法都没有实际使用您的Equals(object)覆盖。要使用它,请写:

Assert.AreEqual( ... );

修改:我认为Assert.AreEqual(exp, act);总是会exp.Equals(act)执行AppointmentCollection,这会在ICollection上调用您的覆盖。但事实证明我们以私有instance method EqualConstraint.ObjectsEqual结束,并且正如人们所看到的那样,它会检查运行时类型是否实现Assert.AreEqual,在这种情况下,您的覆盖永远不会被使用。

获得的经验:使用CollectionAssert.AreEquivalent可能会使收藏混淆。使用CollectionAssert.AreEqualEquals可以明确您的意图。如果您只需要进行测试,则无需覆盖AppointmentCollection上的list1.Equals(list2)。如果您需要应用程序本身并且想要测试它,请用Appointment字面写下测试,以确保您自己的覆盖是经过测试的。

(无论如何,当然需要覆盖{{1}}。)