mercredi 4 mai 2016

Unit testing XML deserialisation

I am new to unit testing and I am wondering what would be the best practices for unit testing xml deserialisation.

Consider the following xml:

<people>
  <person id="1">
    <name>Joe</name>
    <age>28</age>
  </person>
  <person id="2">
    <name>Jack</name>
    <age>38</age>
  </person>
</people>

And the following model class for the 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; }
    }

I deserialize the xml using:

        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;
        }

What would be the best methods to unit test the GetListOfPeople method above?

Aucun commentaire:

Enregistrer un commentaire