lundi 25 avril 2016

Unit test a method that sorts a collection

I have a method that sorts a collection based on a property like this:

public List<Student> GetAllStudents()
{
    return _studentCatalogContext.Student.Where(x => (x.Course != 2 && x.Course != 6)).OrderByDescending(x => x.EnrollDateTime).ToList();
}

So the idea is to have, in this case, the recent enrolled Student first.

Since the result of the method call will be a sorted list with the newest enrollment first I wrote the test like this:

[TestMethod]
public void Calling_GetAllStudents_ReturnsSortedListOfStudents()
{
    var studentsList = new List<Student> {
    new Student {
                     Id = "123",
                     EnrollTime = "02/22/16 14:06:56 PM",
                     Course = 1
                 },
     new Student {
                     Id = "456",
                     EnrollTime = "03/30/16 12:50:38 PM",
                     Course = 3
                 }
                 };

    _studnentRepository.Setup(x=>x.GetAllStudents()).Returns(studentsList);

    Assert.AreEqual("02/22/16 14:06:56 PM", studentsList[0].EnrollTime);
}

It's been suggested that this test does not hold the purpose in that it sets a value and asserts on it.

How would I write a correct unit test in this case?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire