FizzBuzz Kata
Implement the FizzBuzz kata to gain familiarity with the basics of the TDD approach.
We'll cover the following
We’ll start practicing with the TDD approach. In this group of lessons, we’ll build a kata by following this technique. The example kata we are using is the FizzBuzz
kata.
Before starting, let’s review its rules. The FizzBuzz
function accepts an integer as a parameter, and it returns a string. The latter depends on the following conditions:
- If the number is divisible by three, it returns
Fizz
. - If the number is divisible by five, it returns
Buzz
. - If the number is divisible by three and five, it returns
FizzBuzz
. - If the number is neither divisible by three nor by five, it returns a string representation of that number.
In this kata, it’s clear that we have four requirements (or features) to implement. By implementing each of them with the TDD technique, we’ll gain familiarity with this approach and be ready to deal with more complex projects.
The basic scenario
The TDD approach forces us to think about the most basic subset of the problem. After solving it, we can move on and build other features. To build every feature, we have to go through all of the stages of the TDD which are Red, Green, and Refactor.
The Red phase
Let’s see what our starting point looks like:
Get hands-on with 1200+ tech skills courses.