我目前正在尝试学习如何使用单元测试,并且我已经创建了 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);
}
这是正确的,因为列表可能看起来相同,它们是包含相同数据的 2 个不同对象。
为了比较列表,您应该使用集合断言
CollectionAssert.AreEqual(expected, actual);
这应该够了吧。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)