Upload
jordan-poulton
View
479
Download
2
Tags:
Embed Size (px)
DESCRIPTION
A short tutorial on how to solve the FizzBuzz Kata, using Test Driven Development & Ruby. The challenge is to write a piece of Test Driven code that gives the correct response according to the rules of the game Fizzbuzz. The rules are that players count incrementally, replacing any number divisible by 3 with the word "Fizz", any number divisible by 5 with the word "Buzz", and any number divisible by both 3 AND 5 with the word "FizzBuzz". The TDD (Test Driven Development) is done with RSpec, and Guard gives instant notifications. The original video was created by Enrique Comba Riepenhausen, Lead Coach at Makers Academy. You can view it at: https://www.youtube.com/watch?v=CHTep2zQVAc&feature=youtu.be Makers Academy is 12 week Web Developer Bootcamp based in the heart of London's Silicon Roundabout. If you're interested in learning to code, check out www.MakersAcademy.com. For more great resources, subscribe to our Youtube channel or go to www.MakersAcademy.com and sign up to our newsletter.
Citation preview
The Rules of FizzBuzzIf number is divisible by 3, return “Fizz” If number is divisible by 5, return “Buzz”
If number is divisible by 3 & 5, return “FizzBuzz” Otherwise, return the number
TDDRed - Green - Refactor
Underlying ConceptsAll tests should pass
Code should be clear, consistent, minimal, but also expressive Code should be DRY, avoiding repetition
How to Get to GreenFake it - return what the test requires, nothing more
Obvious - when the code to make the test pass is trivial Triangulation - write a second test forcing the ‘fake’ solution to
be exposed as insufficient
Key for this tutorialsample_spec.rb
sample_spec.rb
sample_spec.rb
= test will error out
= failing test
= passing test
StartYou will need to initialize RSpec
We recommend having Guard installed You should create a fizzbuzz_spec.rb file in the ./spec directory
fizzbuzz_spec.rb
fizzbuzz_spec.rb
You should create a ./lib directory And a fizzbuzz.rb file inside it
Ideally you should do this from the command line… !
Practise your Command Line Fu :)
fizzbuzz_spec.rb
fizzbuzz_spec.rb
We have our first failing example!
fizzbuzz_spec.rb
We have our first failing example!
fizzbuzz_spec.rb
Now - just focus on changing the failing test’s message:
fizzbuzz_spec.rb
Let the tests drive your code!
fizzbuzz_spec.rb
Let the tests drive your code!
fizzbuzz_spec.rb
Keep making small, incremental changes. Just focus on changing the message each time… Nothing more.
fizzbuzz_spec.rb
Now we have a failing expectation :)
fizzbuzz_spec.rb
Now we have a failing expectation :)
fizzbuzz_spec.rb
There is a lot of debate around the next step… !
We believe that in the spirit of “write the simplest code that could possibly work and makes the test pass”,
You should ‘fake it’ at this point, then ‘triangulate’ on the next step
fizzbuzz_spec.rb
We’re green! (Even though the code is rubbish…)
ButThe code as it currently stands is trivial, and does nothing. We
will need to ‘triangulate’ with another test in order to force ourselves / our pairing partner to write the correct code.
fizzbuzz_spec.rb
fizzbuzz_spec.rb
We now have two tests. Both cannot be true at the same time - this is what many refer to as ‘triangulation’
fizzbuzz_spec.rb
Due to our strategy of triangulation, we’re now forced to write code that satisfies both tests:
fizzbuzz_spec.rb
We now have two passing tests, and the first part of our code - the check to see if a number is divisible by three - is solved.
We’ll no longer show the Command Line. You can use the colours in the bottom corner to keep track of whether your tests should be passing
or failing…
fizzbuzz_spec.rb
fizzbuzz_spec.rb
fizzbuzz_spec.rb
fizzbuzz_spec.rb
fizzbuzz_spec.rb
Triangulating again :)
fizzbuzz_spec.rb
Now we’re getting there :)
fizzbuzz_spec.rb
Things that are divisible by 3 & 5 == things that are divisible by 15
fizzbuzz_spec.rb
fizzbuzz_spec.rb
fizzbuzz_spec.rb
fizzbuzz_spec.rb
And back to triangulating
fizzbuzz_spec.rb
TDDRed - Green - Refactor
fizzbuzz_spec.rb
fizzbuzz_spec.rb
fizzbuzz_spec.rb
fizzbuzz_spec.rb
fizzbuzz_spec.rb
fizzbuzz_spec.rb
TDDWhen refactoring, you should always stay ‘in the green’
!At no point should your tests start failing. If they do, you should
rectify the problem immediately
TDDNow, back to the Red - Green - Refactor cycle
fizzbuzz_spec.rb
Let’s write the test for the actual gameplay
fizzbuzz_spec.rb
fizzbuzz_spec.rb
fizzbuzz_spec.rb
fizzbuzz_spec.rb
fizzbuzz_spec.rb
fizzbuzz_spec.rb
fizzbuzz_spec.rb
fizzbuzz_spec.rb
fizzbuzz_spec.rb
That’s it!Now go to the Command Line, and launch irb
Drumroll please!drdrdrdrdrdrdrdrdrdrdrdrdrdrdrdrdrdrdrdrdrrrrrrr…..
…
To see this being coded before your very eyes, visit:
bit.ly/FizzBuzz_TDD