intro to ruby and test-driven development - fizzbuzz kata - by makers academy

63

Upload: jordan-poulton

Post on 26-May-2015

481 views

Category:

Technology


2 download

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.

TRANSCRIPT

Page 1: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy
Page 2: 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

Page 3: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

TDDRed - Green - Refactor

Page 4: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

Underlying ConceptsAll tests should pass

Code should be clear, consistent, minimal, but also expressive Code should be DRY, avoiding repetition

Page 5: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

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

Page 6: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

Key for this tutorialsample_spec.rb

sample_spec.rb

sample_spec.rb

= test will error out

= failing test

= passing test

Page 7: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

StartYou will need to initialize RSpec

We recommend having Guard installed You should create a fizzbuzz_spec.rb file in the ./spec directory

Page 8: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 9: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

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 :)

Page 10: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 11: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

We have our first failing example!

Page 12: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

We have our first failing example!

Page 13: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Now - just focus on changing the failing test’s message:

Page 14: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Let the tests drive your code!

Page 15: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Let the tests drive your code!

Page 16: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Keep making small, incremental changes. Just focus on changing the message each time… Nothing more.

Page 17: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Now we have a failing expectation :)

Page 18: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Now we have a failing expectation :)

Page 19: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

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

Page 20: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

We’re green! (Even though the code is rubbish…)

Page 21: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

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.

Page 22: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 23: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

We now have two tests. Both cannot be true at the same time - this is what many refer to as ‘triangulation’

Page 24: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Due to our strategy of triangulation, we’re now forced to write code that satisfies both tests:

Page 25: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

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…

Page 26: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 27: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 28: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 29: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 30: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Triangulating again :)

Page 31: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Now we’re getting there :)

Page 32: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Things that are divisible by 3 & 5 == things that are divisible by 15

Page 33: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 34: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 35: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 36: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

And back to triangulating

Page 37: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 38: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

TDDRed - Green - Refactor

Page 39: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 40: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 41: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 42: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 43: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 44: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 45: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

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

Page 46: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

TDDNow, back to the Red - Green - Refactor cycle

Page 47: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Let’s write the test for the actual gameplay

Page 48: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 49: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 50: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 51: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 52: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 53: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 54: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 55: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 56: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

fizzbuzz_spec.rb

Page 57: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

That’s it!Now go to the Command Line, and launch irb

Page 58: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy
Page 59: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

Drumroll please!drdrdrdrdrdrdrdrdrdrdrdrdrdrdrdrdrdrdrdrdrrrrrrr…..

Page 60: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

Page 61: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy
Page 62: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy

To see this being coded before your very eyes, visit:

bit.ly/FizzBuzz_TDD

Page 63: Intro to Ruby and Test-Driven Development - FizzBuzz Kata - By Makers Academy