Code Editor
Terminal
Learn
Learn TDD With Mocha

Red To Green I

Congrats you're "in the red!"

Now let's learn how to fix the error and move into the green. The "red" error messages serve as instructions for you to start writing your production code.

Let's look at the error message from our Phrase.initials() example in the last exercise:

1) Phrase .initials should return the first letter of each word in a phrase.:
ReferenceError: Phrase is not defined

The reference error tells us that Phrase is not defined. If we were implementing this, we would start by creating a Phrase object:

const Phrase = {}

The new error would now say:

1) Phrase .initials should return the first letter of each word in a phrase.:
TypeError: Phrase.initials is not a function

We're still in the red, but we have a new error. The error says that Phrase.initials is not a function. That's because we haven't added an initials function to the Phrase module. Let's do that now:

const Phrase = { initials() { } }

Now when we run our test we get the following error:

1) Phrase .initials should return the first letter of each word in a phrase.:
AssertionError: 'NM' == undefined

This is the first error that is failing the intended behavior. Let's make one more change to get our test to pass:

const Phrase = { initials(phr) { return 'NM'; } }

Finally, we got our test to pass! But that doesn't mean our method is working as intended. If we entered "Jody Williams" our method would return 'NM', not 'JW'.

That's okay! We'll write another test once we've refactored our test and production code.


Now, you are going to return to yourCalculate module and write the minimum possible implementation code to pass the test. You can follow the steps outlined above.

Remember: One of the keys to TDD is that you only write code in response to the error message that your test revealed. Make sure you are addressing one behavior at a time, as we did here.

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