我怎样才能做到这一点的方法的单元测试我该如何做这个方法的单元测试?
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的,但我有 通用ICollections。我需要做什么?
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);
}
我的方法必须返回集合像预期becouse从“乌克兰”我所有的人,但测试没有通过..
就是了预期的收集和实际的。我认为你使用泛型集合并不重要。 –