Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy
-
Upload
jordan-poulton -
Category
Technology
-
view
479 -
download
2
description
Transcript of Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy
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