我怎樣才能做到這一點的方法的單元測試我該如何做這個方法的單元測試?
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從「烏克蘭」我所有的人,但測試沒有通過..
就是了預期的收集和實際的。我認爲你使用泛型集合並不重要。 –