Failing tests as a reminder of unimplemented requirement
Tuesday, December 16th, 2008I am a new comer to the art of unit testing and it has not yet become the part of my coding routine. However, even this early I have realized how useful writing unit tests can be. Specially writing unit tests gives you the understanding how the class will be used and where the errors can arise etc. For example passing the Type System.String to a method where the method expects a Type which implements a particular interface ICommand should result in an error because the System.String does not certainly implements the interface ICommand. But does the method validate the input?
Now writing a test for this case is very simple. Just create the object, call the method with System.String as the Type parameter.
[Test]
[ExpectedException(typeof(ArgumentException))]
public void CanNotRegisterTypesNotImplementingICommand()
{
CommandLocater locater=new CommandLocater();
locater.RegisterCommand(1,typeof(string));
}
Certainly I haven’t implemented the validation code for checking this, so the test is certainly failing. Until I feel like doing this validation, this test will always fail. Lot better than forgetting to write this code and getting an unrelated exception in any other section of the code is getting a failing test which will make sure that I’ll write the code necessary to implement this requirement.
“Always write tests to break your code.”