gpt4 book ai didi

c# - 我怎样才能对这种方法进行单元测试?

转载 作者:行者123 更新时间:2023-11-30 14:31:55 27 4
gpt4 key购买 nike

如何对该方法进行单元测试

public static ICollection<Person> SelectPersonByCountry(string Country, LinkedList<Person> personList)  
{
ICollection<Person> selectedPerson = new List<Person>();
if (Country != String.Empty)
{
foreach (Person item in personList)
{
if (item.Country.ToUpper().Equals(Country.ToUpper()))
{
selectedPerson.Add(item);
}
}
}
else
{
// do something
return null;
}
return selectedPerson;

方法 CollectionAssert.AreEqual() 需要 2 个参数 ICollection 和 ICollection 但我有通用 IC 集合。我需要做什么?

public void TestMethod1()
{
string country = "Ukraine";

LinkedList<lab1.Person> personList = new LinkedList<lab1.Person>();
personList.AddFirst(new Person("Dasda", "Sasha", "Ukraine", "23131", "Ukrainian"));
personList.AddFirst(new Person("Sasa", "OLeg", "Ukraine", "23131", "Ukrainian"));
personList.AddFirst(new Person("Popa", "Sveta", "Ukraine", "23131", "Ukrainian"));
personList.AddFirst(new Person("Bezik", "Vitya", "Ukraine", "23131", "Ukrainian"));
personList.AddFirst(new Person("Hoi", "Oleg", "Ukraine", "23131", "Ukrainian"));
ICollection<Person> expected = new LinkedList<Person>();
expected.Add(new Person("Dasda", "Sasha", "Ukraine", "23131", "Ukrainian"));
expected.Add(new Person("Sasa", "OLeg", "Ukraine", "23131", "Ukrainian"));
expected.Add(new Person("Popa", "Sveta", "Ukraine", "23131", "Ukrainian"));
expected.Add(new Person("Bezik", "Vitya", "Ukraine", "23131", "Ukrainian"));
expected.Add(new Person("Hoi", "Oleg", "Ukraine", "23131", "Ukrainian"));


ICollection expected1 = (ICollection)expected;

ICollection actual = (ICollection)lab1.Person.SelectPersonByCountry(country, personList);



CollectionAssert.AreEqual(expected1, actual );



}

我的方法必须像预期的那样返回集合,因为我所有来自“乌克兰”的人,但测试没有通过..

最佳答案

Method CollectionAssert.AreEqual() wants 2 arguments ICollection and ICollection but i have generic ICollections. What do i need to do?

大多数泛型集合还实现了非泛型 ICollection 接口(interface),因此您无论如何都可以传递泛型集合。

ICollection actual = (ICollection)SelectPersonByCountry(country, personList);
ICollection expected = new[] { person1, person2 };

CollectionAssert.AreEqual(actual, expected);

关于c# - 我怎样才能对这种方法进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19289726/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com