断言比较两个对象列表C#

时间:2013-10-24 09:47:22

标签: c# unit-testing tdd assert

我目前正在尝试学习如何使用单元测试,并且我已经创建了3个动物对象的实际列表以及3个动物对象的预期列表。问题是如何断言检查列表是否相等?我尝试过CollectionAssert.AreEqual和Assert.AreEqual,但无济于事。任何帮助将不胜感激。

测试方法:

  [TestMethod]
    public void createAnimalsTest2()
    {
        animalHandler animalHandler = new animalHandler();
        // arrange
        List<Animal> expected = new List<Animal>();
        Animal dog = new Dog("",0);
        Animal cat = new Cat("",0);
        Animal mouse = new Mouse("",0);
        expected.Add(dog);
        expected.Add(cat);
        expected.Add(mouse);
        //actual
        List<Animal> actual = animalHandler.createAnimals("","","",0,0,0);


        //assert
        //this is the line that does not evaluate as true
        Assert.Equals(expected ,actual);

    }

3 个答案:

答案 0 :(得分:4)

只是想让某人在将来遇到这个,答案是我必须创建一个Override,IEqualityComparer,如下所述:

public class MyPersonEqualityComparer : IEqualityComparer<MyPerson>
{
public bool Equals(MyPerson x, MyPerson y)
{
    if (object.ReferenceEquals(x, y)) return true;

    if (object.ReferenceEquals(x, null)||object.ReferenceEquals(y, null)) return false;

    return x.Name == y.Name && x.Age == y.Age;
}

public int GetHashCode(MyPerson obj)
{
    if (object.ReferenceEquals(obj, null)) return 0;

    int hashCodeName = obj.Name == null ? 0 : obj.Name.GetHashCode();
    int hasCodeAge = obj.Age.GetHashCode();

    return hashCodeName ^ hasCodeAge;
}

}

答案 1 :(得分:2)

这是正确的,因为列表是包含相似数据的2个不同对象。

为了获得比较列表,您应该使用CollectionAssert

CollectionAssert.AreEqual(expected ,actual);

这应该可以解决问题。

答案 2 :(得分:0)

我认为仅出于测试目的而实现IEqualityComparer(Equals()和GetHashCode())是一种代码味道。我宁愿使用以下断言方法,您可以在其中自由定义要在哪些属性上进行断言:

public static void AssertListEquals<TE, TA>(Action<TE, TA> asserter, IEnumerable<TE> expected, IEnumerable<TA> actual)
{
    IList<TA> actualList = actual.ToList();
    IList<TE> expectedList = expected.ToList();

    Assert.True(
        actualList.Count == expectedList.Count,
        $"Lists have different sizes. Expected list: {expectedList.Count}, actual list: {actualList.Count}");

    for (var i = 0; i < expectedList.Count; i++)
    {
        try
        {
            asserter.Invoke(expectedList[i], actualList[i]);
        }
        catch (Exception e)
        {
            Assert.True(false, $"Assertion failed because: {e.Message}");
        }
    }
}

实际情况如下:

    public void TestMethod()
    {
        //Arrange
        //...

        //Act
        //...

        //Assert
        AssertAnimals(expectedAnimals, actualAnimals);
    }

    private void AssertAnimals(IEnumerable<Animal> expectedAnimals, IEnumerable<Animal> actualAnimals)
    {
        ListAsserter.AssertListEquals(
            (e,a) => AssertAnimal(e,a),
            expectedAnimals,
            actualAnimals);
    }

    private void AssertAnimal(Animal expected, Animal actual)
    {
        Assert.Equal(expected.Name, actual.Name);
        Assert.Equal(expected.Weight, actual.Weight);
        //Additional properties to assert...
    }

我将XUnit用于简单的Assert.True(...)和Assert.Equals(),但是您可以使用其他任何单元测试库。希望它能对某人有所帮助! ;)

相关问题