lundi 30 mai 2016

Web api Unit testing for required fields present in json response

One of my .Net web api endpoint returns a JSON object of following structre

    public class UserModel
    {
        public int StudentID { get; set; }
        public int ClassID { get; set; }
        public string ClassName { get; set; }
        public int TestLevelID { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public string Email { get; set; }        
        public int? Active { get; set; }
        public string Displayname { get
            {
                return Firstname + " " + Lastname;
            }
        }
        public List<TestSummary> CourseSummary { get; set; }
    }

    public class TestSummary
    {
        public string Coursename { get; set; }
        public int Progressvalue { get; set; }
    }

How to implement the unit Test method in a way that checks for all properties are correct ? Required fields contain non-null data and optional field contains at least null

    [TestMethod]
    public void GetStudent()
    {
        var service = new StudentDbHandler();
        var students = service.GetStudents(1).ToList();
        bool _exists = false;
        if (students.Count > 0)
        {
            _exists = true;
        }
        Assert.IsTrue(_exists);
        //now checls first name is not null
        Assert.IsNotNull(students.FirstOrDefault().Firstname);
    }

So should i do this for all properties or is any easier way

Aucun commentaire:

Enregistrer un commentaire