Man on the Moon - 6/7
Chandrayaan - 3

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:

  1. Arrange the object you want to test;
  2. Act on the object; and
  3. Assert that the action went as expected.

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.

要查看或添加评论,请登录

Anilesh Sharma的更多文章

  • Man on the Moon - 7/7

    Man on the Moon - 7/7

    Part - 6 is here. Recursive application of PPP We can assume that creating a rocket object is not a single-line…

  • Man on the Moon - 5/7

    Man on the Moon - 5/7

    Part - 4 is here. Maintaining consistent abstractions If, at any time, for any method, we need more than seven plain…

  • Man on the Moon - 4/7

    Man on the Moon - 4/7

    Part - 3 is here. We can delve into the first comment - “// Take a man” - and write code for it.

  • Man on the Moon - 3/7

    Man on the Moon - 3/7

    Part - 2 is here. Pseudocode Programming Process Instead of jumping into writing code, start with writing steps in…

  • Man on the Moon - 2/7

    Man on the Moon - 2/7

    Part - 1 is here. Writing code for humans Simple explanations are a strong marker of clear understanding.

  • Man on the Moon - 1/7

    Man on the Moon - 1/7

    We need to (1) take a man, (2) put him on the moon, and (3) bring him back. This is not an attributable quote, and I am…

  • Peering into Data Management - 5/5

    Peering into Data Management - 5/5

    Part - 4 is here. Surprise Quiz, folks.

  • Peering into Data Management - 4/5

    Peering into Data Management - 4/5

    Part - 3 is here. Since the DBMS architecture is mature, there are two ways to create a new DBMS: Making changes to…

  • Peering into Data Management - 3/5

    Peering into Data Management - 3/5

    Part - 2 is here. The architecture of a DBMS Here is the high-level architecture of a DBMS.

  • Peering into Data Management - 2/5

    Peering into Data Management - 2/5

    Part - 1 is here. DBMS taxonomies Unless developing a DBMS, most devs are okay with a surface-level understanding of…

社区洞察

其他会员也浏览了