Unit testing a list or collection

I have recently starting drinking the Unit Testing koolaid, and I have even put on a robe and started ascending the steps of the unit testing temple. Our test team has a set of standard behaviors they expect when clicking through an app, and one of them is having any lists in order, unless documented otherwise. In a gradual transition and introduction to unit testing I have started writing tests to verify bugs in an effort to eliminate bugs, and ease the always present regression risk.

Here are a few simple examples using a person object, returning a list of people in various orders.

public class People
{
    private List<Person> Persons { get; set; }

    private People()
    {
        Persons = new List<Person>();
        AddDefaultPeople();
    }

    public static IEnumerable<Person> OrderedPersonsLastName()
    {
        return new People().Persons.OrderBy(p => p.LastName).ToList();
    }

    public static IEnumerable<Person> OrderedPersonsFirstName()
    {
        return new People().Persons.OrderBy(p => p.FirstName).ToList();
    }

    public static IEnumerable<Person> GetPeople()
    {
        return new People().Persons;
    }

    public static IEnumerable<Person> OrderedPeopleIdDescending()
    {
        return new People().Persons.OrderByDescending(p => p.PersonId).ToList();
    }

    public static IEnumerable<Person> OrderedPeopleId()
    {
        return new People().Persons.OrderBy(p => p.PersonId).ToList();
    }

    private void AddDefaultPeople()
    {
        Persons.Add(Person.CreatePerson(1, "Tim", "Sneed"));
        Persons.Add(Person.CreatePerson(2, "Dirk", "Pitt"));
        Persons.Add(Person.CreatePerson(3, "Jack", "Ryan"));
        Persons.Add(Person.CreatePerson(4, "Mitch", "Rapp"));
        Persons.Add(Person.CreatePerson(5, "Thomas", "Jefferson"));
    }
}

public class Person
{
    public int PersonId { get; private set; }
    public string FirstName { get; private set; }
    public string LastName { get; private set; }

    private Person()
    {
    }

    public static Person CreatePerson(int personId, string firstName, string LastName)
    {
        return new Person() { FirstName = firstName, LastName = LastName, PersonId = personId };
    }
}

There are numerous ways to test a list and verify that it is in the order you would expect. I started out with a very basic example, filtering through each item and making sure that the item in each position in the collection matches the expected item.

Verify test is working
I first wanted to verify that the lists are not the same, to establish some sort of baseline.

I see this as an immature way to verify a list is in order
This worked, but I definitely thought I was doing way too much to test and verify a simple list was in order.

Expected to work, turns out it does.
I think this is a very straightforward way to do a test. I am used to setting up my tests in this manner with a simple Assert.AreEqual statement.

Collections Assert
CollectionsAssert is a way to test collections in c#. CollectionsAssert works with generics that implement ICollection, turns out List is one of those generics. Because of this, I do not think this is the best way to test a list, if you are decide to use CollectionsAssert on a collection that does not implement ICollection you can always use the ToList() extension.

My personal preference
This is my favorite way. Using the LINQ extension method. This is a very simple, straightforward way to verify the list is in order and is an elegant solution to my problem.

All good
All of the above ways to go about testing a collection work, I certainly lean towards the third way of verifying a collection is in the expected order.

The code for this post is available here.

comments powered by Disqus