throw new MissingTextException();

time to read 2 min | 257 words

One of the ideas of test first programming is that you shouldn't write code that unless you have a failing test. This can be a problem if you are writing code to make a test pass, when you find something new that you want to add, but you don't have a test for it. I don't like waiting until I am finished with the current test (I forget) and I don't like trying to write a new test in the middle of implementing another one. I understand that some people write stuff down, but I hate that, even pulling notepad for a to do is too distracting.

Let us say we have this code:

[Test]
public void CanAddToDistributerIfHasEnoughSpace()
{
	IDistributer distributer = new NavalDistributer()
		.SetNumberOfContainers(3);
	
	distributer.Add( new Container() );

	Assert.AreEqual( 1, distributer.Containers.Count );
}

And the implementation is:

public class NavaDistributer : IDistributer
{
	int numberOfContainers = 0;

	public ICollection<Container> Containers = new List<Container>();

	public IDistributer SetNumberOfContainers(int numberOfContainers)
	{
		this.numberOfContainers = numberOfContainers;
	}

	public void Add(Container container)
	{
		if(Containers.Count >= numberOfContainers)
			throw new MissingTestException();
		Container.Add(container);
	}
}

The check for going over the allowed number of containers is simply shouting at me to build it. But I don't have a test for it, so I really shouldn't, I don't know what we should do in this case, since I haven't given it enough thought. Putting MissingTestException() (often with a descriptive message) keeps me in the flow of the test and satisfy the "you didn't handle this case" voice.