【发布时间】:2016-05-04 09:31:41
【问题描述】:
我是单元测试的新手,我想知道单元测试 xml 反序列化的最佳实践是什么。
考虑以下 xml:
<people>
<person id="1">
<name>Joe</name>
<age>28</age>
</person>
<person id="2">
<name>Jack</name>
<age>38</age>
</person>
</people>
以及下面的模型类:
[XmlRoot(ElementName ="people")]
public class People
{
public People() { PeopleList = new List<Person>(); }
[XmlElement("person")]
public List<Person> PeopleList { get; set; }
}
public class Person
{
[XmlAttribute("id")]
public int id { get; set; }
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("age")]
public int Age { get; set; }
}
我使用以下方法反序列化 xml:
public List<Person> GetListOfPeople()
{
People plist = new People();
string content;
using (StreamReader sr = new StreamReader(manager.Open("People.xml")))
{
var serializer = new XmlSerializer(typeof(People));
plist = (People)serializer.Deserialize(sr);
}
return plist.PeopleList;
}
对上面的 GetListOfPeople 方法进行单元测试的最佳方法是什么?
【问题讨论】:
标签: c# xml unit-testing xml-deserialization