Man on the Moon - 6/7
Part - 5 is here.
PPP template for Unit Tests
The proof of the pudding is in the eating. TDD needs no endorsement, but PPP is not a theoretical proposal either. Unit testing is blessed with a default template for PPP coding. The next time you have to write a unit test and are unsure of how to code it, use the following ‘AAA’ template:
void unitTestWithADescriptiveName() {
// Arrange
// Act
// Assert
}
In this template, the comments are at the level of intent. You have just described what you want to do, which is:
It’s that simple.
The developer who has to read this code after he finds that his changes are causing a unit test failure will understand very clearly what functionality his change has broken.
Now, for each comment, you either have to write a single line, or 3-5 lines, or a single method call to a refactored method. So, let’s say you want to test that pushing the fire button of the rocket will result in a lift-off. The PPP in the unit test can go as follows:
void testPushingFireButtonResultsInLiftOff () {
// Arrange - Get a rocket with some payload.
// Act - Push the fire button.
// Assert - Rocket has lifted off.
}
Part - 7 is here.