6

我目前正在尝试学习如何使用单元测试,并且我已经创建了 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);

    }
4

4 回答 4

10

这是正确的,因为列表可能看起来相同,它们是包含相同数据的 2 个不同对象。

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

CollectionAssert.AreEqual(expected, actual);

这应该够了吧。

于 2013-10-24T10:05:39.550 回答
7

万一将来有人遇到这个问题,答案是我必须创建一个 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;
}

}

于 2013-11-28T15:42:22.997 回答
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(),但您可以为此使用任何其他单元测试库。希望它可以帮助某人!;)

于 2018-12-10T19:00:55.803 回答
0

我修改了方法 AssertListEquals() 并使用了标准 Assert.All()

    public static void AssertListEquals<TE, TA>(IEnumerable<TE> expected, IEnumerable<TA> actual, Action<TE, TA> asserter)
    {
        if (expected == null && actual == null) return;
        Assert.NotNull(expected);
        Assert.NotNull(actual);

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

        var i = 0;
        Assert.All(expected, e =>
        {
            try
            {
                asserter(e, actual.Skip(i).First());
            }
            finally
            {
                i++;
            }
        });
    }
于 2022-02-08T11:18:09.943 回答