I have already written on getting started with TDD. Let me extend it further and mention in theory what are all the phases involved in a TDD.

Before jumping into this article, I hope you read the getting started with TDD article.

There might be more steps involved in TDD. However we're just going to discuss the phases of TDD and what do we do in each phase.

/**

* @disclaimer

* I'm just preaching what I have learnt & practice

* Some parts of it could be wrong

* Please feel free to leave comments if am wrong

* And I would be happy to stand corrected

*/

The three phases of TDD are the,

- Red phase
- Green phase
- Blue phase

### Red phase

### Green phase

### Blue phase

[Image source - medium.com] |

Write a method that accepts two positive numbers as input and returns their sum

__Case 1 - Function addPositive exists__

__Case 1 - Function addPositive exists__

**Red phase**

- it('should have addPositive method'); - The test case for this would fail at this point.

To take this to the green phase, we need to write code for it.

Therefore, writing code for it,

function addPositive(a,b){}

**Green phase**

- it('should have addPositive method'); - Passes now as we have the method written

There's no refactor required here and so we move on to the red phase of case 2.

__Case 2 - Accepts two numbers as input__

__Case 2 - Accepts two numbers as input__

**Red phase**

- it('should have addPositive method'); - Passes now as we have the method written
- it('should accept two numbers as input'); - The test case for this would fail because it does not throw any error
- it('should not accept non numbers as input'); - The test case for this would fail because it does not throw any error

**Green phase**

- it('should have addPositive method'); - Passes now as we have the method written
- it('should accept two numbers as input'); - Passes now as we have updated the method
- it('should not accept non numbers as input'); - Passes now as we have updated the method

__Case 3 - Accepts two positive numbers as input__

__Case 3 - Accepts two positive numbers as input__

**Red phase**

- it('should have addPositive method'); - Passes now as we have the method written
- it('should accept two numbers as input'); - Passes now as we have updated the method
- it('should not accept non numbers as input'); - Passes now as we have updated the method
- it('should accept two positive numbers as input'); - The test case for this would fail because it does not check if the numbers are positive.
- it('should not accept negative numbers as input'); - The test case for this would fail because it does not check if the numbers are positive.

**Green phase**

- it('should have addPositive method'); - Passes now as we have the method written
- it('should accept two numbers as input'); - Passes now as we have updated the method
- it('should not accept non numbers as input'); - Passes now as we have updated the method
- it('should accept two positive numbers as input'); - Passes now as we have updated the method
- it('should not accept negative numbers as input'); - Passes now as we have updated the method

**Blue phase**

- it('should have addPositive method'); - Passes now as we have the method written
- it('should accept two numbers as input'); - Passes now as we have updated the method
- it('should not accept non numbers as input'); - Passes now as we have updated the method
- it('should accept two positive numbers as input'); - Passes now as we have updated the method
- it('should not accept negative numbers as input'); - Passes now as we have updated the method

- it('should have addPositive method'); - Passes now as we have the method written
- it('should accept two positive numbers as input'); - Passes now as we have updated the method
- it('should not accept negative numbers as input'); - Passes now as we have updated the method

__Case 4 - Returns sum of the numbers__

__Case 4 - Returns sum of the numbers__

**Red phase**

- it('should have addPositive method'); - Passes now as we have the method written
- it('should accept two positive numbers as input'); - Passes now as we have updated the method
- it('should not accept negative numbers as input'); - Passes now as we have updated the method
- it('should return sum of two numbers'); - The test case for this would fail because it does not return the sum yet.

**Green phase**

- it('should have addPositive method'); - Passes now as we have the method written
- it('should accept two positive numbers as input'); - Passes now as we have updated the method
- it('should not accept negative numbers as input'); - Passes now as we have updated the method
- it('should return sum of two numbers'); - Passes now as we have updated the method to return sum

**Blue phase**

- it('should have addPositive method'); - Passes now as we have the method written
- it('should accept two positive numbers as input'); - Passes now as we have updated the method
- it('should not accept negative numbers as input'); - Passes now as we have updated the method
- it('should return sum of two numbers'); - Passes now as we have updated the method to return sum

**Green phase**

- it('should have addPositive method'); - Passes now as we have the method written
- it('should return sum by accepting two positive numbers as input'); - Passes now because it accepts two positive numbers as input and returns the sum as expected
- it('should not accept negative numbers as input'); - The test case passes because our method now throws error when the input is either negative or if it is non-number.

__Case 5 - Input number is greater than max value__

__Case 5 - Input number is greater than max value__

**Red phase**

- it('should have addPositive method'); - Passes now as we have the method written
- it('should accept two positive numbers as input'); - Passes now as we have updated the method
- it('should not accept negative numbers as input'); - Passes now as we have updated the method
- it('should return sum of two numbers'); - Passes now as we have updated the method to return sum
- it('should throw error if input value is higher than max value'); - The test case for this would fail because in the code, we do not test the input against max value

**Green phase**

- it('should have addPositive method'); - Passes now as we have the method written
- it('should accept two positive numbers as input'); - Passes now as we have updated the method
- it('should not accept negative numbers as input'); - Passes now as we have updated the method
- it('should return sum of two numbers'); - Passes now as we have updated the method to return sum
- it('should throw error if input value is higher than max value'); - Passes now as we have updated the method to throw error if input is not under max value

## Comments

## Post a Comment