For a developer who practices TDD, the red, green, refactor flow is nothing new. In this post I want to stand up for red, the great misunderstood one. To illustrate it I’ll use a very simple example:

[TestMethod]
public void sample_test()
{
    var messagingService = GivenAMessagingServiceStub();
    var unreadMessages = messagingService.UnreadMessages();
    unreadMessages.Count.Should().Be(1)
}

This example is simplified, but it lets me show a test with a given/when/then or AAA structure (pick your favorite). In order to get that first red, sometimes I come across this:

public IEnumerable<Message> UnreadMessages()
{
    throw new NotImplementedException();
}

I think doing this defeats the purpose of looking for the red. One of the goals of getting that first red is to verify the test, but it’s impossible to verify the test if it never runs all the way through. Sure, running the test will give a red, but that red is the same as having thrown the exception instead of calling the production code, so it’s not worth much. Other times people go a bit further in the search for red with code like this:

public IEnumerable<Message> UnreadMessages()
{
    return null;
}

In this case the test does fail on the then line, but personally I’m still not convinced, because the red isn’t actually caused by the assert. The reason is simple and is illustrated pretty well by dissecting the test like this:

[TestMethod]
public void sample_test()
{
    var messagingService = GivenAMessagingServiceStub();
    var unreadMessages = messagingService.UnreadMessages();
    var count = unreadMessages.Count;
    count.Should().Be(1)
}

With this example you can clearly see that the red isn’t due to the assert, it’s due to a NullPointerException when assigning the count variable, which is still far from the goal of testing the test first. In this case, it doesn’t take much to find a red that also lets us exercise the whole test:

public IEnumerable<Message> UnreadMessages()
{
    return new List<Message>();
}

Of course, it’s important not to forget that the first step is never red, it’s thinking.