Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to TDD: Red-Green-Refactor

Introduction to TDD: Red-Green-Refactor

Fran Iglesias

November 02, 2018
Tweet

More Decks by Fran Iglesias

Other Decks in Programming

Transcript

  1. Test Driven Development It’s a discipline Unit test first Only

    code to pass the test Loop of minimal changes
  2. TDD laws You can't write any production code until you

    have first written a failing unit test. You can't write more of a unit test than is sufficient to fail, and not compiling is failing. You can't write more production code than is sufficient to pass the currently failing unit test. Robert C. Martin
  3. TDD cycle Write a failing test Write just enough code

    to make the test pass Refactor if needed
 (mostly reduce duplication)
  4. Write a failing test Write a test to prove a

    simple case Run the test and see how it fails Errors == test fails
  5. Make the test pass Write production code until: • all

    errors are fixed • the test passes
  6. Write a failing test class ValidateNifTest extends TestCase { public

    function testRealNIFShouldBeValid() { $validateNif = new ValidateNif(); $this->assertTrue($validateNif->isValid('00000000T')); } } Error : Class 'Dojo\ValidateNif\ValidateNif' not found /Users/franiglesias/PhpstormProjects/dojo/tests/Dojo/ValidateNif/ValidateNifTest.php: 13
  7. Make the test pass class ValidateNif { } Error :

    Call to undefined method Dojo\ValidateNif\ValidateNif::isValid() /Users/franiglesias/PhpstormProjects/dojo/tests/Dojo/ValidateNif/ValidateNifTest.php: 14
  8. Make the test pass Failed asserting that null is true.

    /Users/franiglesias/PhpstormProjects/dojo/tests/Dojo/ValidateNif/ValidateNifTest.php: 14 class ValidateNif { public function isValid(string $nif) { } }
  9. Make the test pass OK (1 test, 1 assertion) class

    ValidateNif { public function isValid(string $nif):bool { return true; } }
  10. Fizz Buzz Kata stage 1 Write a program that prints

    the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz “. http://codingdojo.org/kata/FizzBuzz/
  11. Fizz Buzz Kata
 stage 2 A number is “Fizz" if

    it is divisible by 3 or if it has a 3 in it A number is “Buzz” if it is divisible by 5 or if it has a 5 in it http://codingdojo.org/kata/FizzBuzz/
  12. Fizz Buzz Kata
 stage 3 We want all these rules

    configurable. http://codingdojo.org/kata/FizzBuzz/