Drag the edges to resize the window.

Code Editor
Terminal
Learn
Learn TDD With Mocha

Getting into the Red II

Congratulations, you have made it through your first red-green-refactor cycle using TDD! The next step is to repeat this cycle to build a more complete implementation of the .sum method.

Now we are ready to get back into the red. We need to write a test that pushes the implementation of our .sum method.

Once you have a baseline test for your feature, you can start to write additional test cases that force you to write better implementation code.

Let's consider the test suite for our Phrase.initial method. We have one test that checks if Phrase.initial("Nelson Mandela") returns "NM".

describe('Phrase', () => { describe('.initials', () => { it('returns the first letter of each word in a phrase.', () => { const nameInput = 'Nelson Mandela'; const expectedInitials = 'NM'; const result = Phrase.intials(inputName); assert.equal(result, expectedInitials); }) }) })

Let's write another test that pushes us to implement a Phrase.initials() method that returns the first letter of each word for "Nelson Mandela" and another person.

To do this, we will add another it block to our code that follows the setup, exercise, verification framework for writing tests.

describe('Phrase', () => { describe('.initials', () => { . . . it('returns the first letter of each word in a phrase.', () => { const nameInput = 'Juan Manuel Santos'; const expectedInitials = 'JMS'; const result = Phrase.initials(nameInput); assert.equal(result, expectedInitials); }) }) })

Although the new "it" block is similar to the existing example, the two together force you to implement a method that will return initials that pass both.

The idea of TDD is to continue building tests and going through the red-green-refactor process until you feel confident that your implementation code performs as expected.

Report a Bug
If you see a bug or any other issue with this page, please report it here.