Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Test Driven Development	<br />Why I hate it, but really love it.<br />
Start with FAIL<br />
Make it Green!<br />
Make it Pretty (refactor)<br />(Not Like this chap)<br />
Eh?<br />Why???<br />How???<br />When???<br />
						WHY TDD?<br />
Stability<br />
Flexibility<br />
Documentation<br />
MORE SPEEEEDzzz<br />
Design<br />
HOW TDD?<br />(Disclaimer: Code incoming!)<br />
Writing an adding, er.. Thing…<br />Failing Test:<br />def test_add_1_plus_1<br />assert_equal 2, add(1,1)<br />end<br />O...
WHATS THE NEXT STEP?<br />
Make it pass:<br />def add(i,j)<br />   return 2<br />end<br />Output:<br />Loaded suite TddExample<br />Started<br />.<br...
Waait!! <br />Why isn’t it just:<br />def add(i,j)<br />   return i+j<br />end<br />
WHATS THE NEXT STEP?<br />
Write another failing test:<br />def test_add_1_plus_2<br />assert_equal 3, add(1,2)<br />End<br />Loaded suite TddExample...
Make it pass:<br />def add(i,j)<br />  return i+j<br />End<br />Loaded suite TddExample<br />Started<br />..<br />Finished...
All about the baby steps<br />
Problems?<br />
add(1,2,3)<br />Fail.<br />add(1,”two”)<br />Fail.<br />
How do we cope?<br />
Any number of arguments?<br />First add a test..<br />def test_adding_three_arguments<br />assert_equal 4, add(1,2,1)<br /...
Refactor<br />For the general solution:<br />def add(*args)<br />  return args.inject(0){|sum,n| sum + n}<br />End<br />Lo...
Dependency Injection<br />(This has nothing to do with children)<br />
Example:<br />class PersonDAO<br />  def initialize(user, password)<br />    @data_source = Database.connect(user, passwor...
Why is this better?<br />
And you’re telling me this… why?<br />
Start off with a test<br />def setup<br />  @person = FamilGuyDao.new<br />end<br />def test_return_full_name_is_reginald_...
For now we’ll pass in the name<br />
Next Test….<br />
Make it pass…<br />
WHATS THE NEXT STEP?<br />
The General Case<br />Integration faked by Mock object!<br />
MAKE IT PASS!!<br />
DONE!<br />
Upcoming SlideShare
Loading in …5
×

Test Driven Development: Why I hate it; but secretly love it.

1,169 views

Published on

Published in: Real Estate, Technology, Business
  • Be the first to comment

  • Be the first to like this

Test Driven Development: Why I hate it; but secretly love it.

  1. 1. Test Driven Development <br />Why I hate it, but really love it.<br />
  2. 2. Start with FAIL<br />
  3. 3. Make it Green!<br />
  4. 4. Make it Pretty (refactor)<br />(Not Like this chap)<br />
  5. 5. Eh?<br />Why???<br />How???<br />When???<br />
  6. 6. WHY TDD?<br />
  7. 7. Stability<br />
  8. 8. Flexibility<br />
  9. 9. Documentation<br />
  10. 10. MORE SPEEEEDzzz<br />
  11. 11. Design<br />
  12. 12. HOW TDD?<br />(Disclaimer: Code incoming!)<br />
  13. 13. Writing an adding, er.. Thing…<br />Failing Test:<br />def test_add_1_plus_1<br />assert_equal 2, add(1,1)<br />end<br />Output:<br />tdd.rb:6:in `test_add_1_plus_1': undefined method `add' for main:Object (NoMethodError)<br />from tdd.rb:9<br />
  14. 14. WHATS THE NEXT STEP?<br />
  15. 15. Make it pass:<br />def add(i,j)<br /> return 2<br />end<br />Output:<br />Loaded suite TddExample<br />Started<br />.<br />Finished in 0.000455 seconds.<br /> 1 tests, 1 assertions, 0 failures, 0 errors<br />
  16. 16. Waait!! <br />Why isn’t it just:<br />def add(i,j)<br /> return i+j<br />end<br />
  17. 17. WHATS THE NEXT STEP?<br />
  18. 18. Write another failing test:<br />def test_add_1_plus_2<br />assert_equal 3, add(1,2)<br />End<br />Loaded suite TddExample<br />Started<br />F<br />Finished in 0.042739 seconds.<br />1) Failure:<br />test_add_1_plus_1(TddExample) [tdd.rb:42]:<br /><3> expected but was<br /><2>.<br />1 tests, 1 assertions, 1 failures, 0 errors<br />
  19. 19. Make it pass:<br />def add(i,j)<br /> return i+j<br />End<br />Loaded suite TddExample<br />Started<br />..<br />Finished in 0.000462 seconds.<br />2 tests, 2 assertions, 0 failures, 0 errors<br />
  20. 20. All about the baby steps<br />
  21. 21. Problems?<br />
  22. 22. add(1,2,3)<br />Fail.<br />add(1,”two”)<br />Fail.<br />
  23. 23. How do we cope?<br />
  24. 24. Any number of arguments?<br />First add a test..<br />def test_adding_three_arguments<br />assert_equal 4, add(1,2,1)<br />End<br />Make it pass!!<br />Add another test!!<br />def test_adding_four_arguments<br />assert_equal 8, add(1,2,1,4)<br />end<br />
  25. 25. Refactor<br />For the general solution:<br />def add(*args)<br /> return args.inject(0){|sum,n| sum + n}<br />End<br />Loaded suite TddExample<br />Started<br />....<br />Finished in 0.000941 seconds.<br />4 tests, 4 assertions, 0 failures, 0 errors<br />
  26. 26. Dependency Injection<br />(This has nothing to do with children)<br />
  27. 27. Example:<br />class PersonDAO<br /> def initialize(user, password)<br /> @data_source = Database.connect(user, password)<br /> end<br />end<br />VS<br />class PersonDAO<br /> def initialize(data_source)<br /> @data_source = data_source<br /> end<br />end<br />
  28. 28. Why is this better?<br />
  29. 29. And you’re telling me this… why?<br />
  30. 30. Start off with a test<br />def setup<br /> @person = FamilGuyDao.new<br />end<br />def test_return_full_name_is_reginald_von_griffen<br />assert_equal "Reginald Von-Griffin", @person.full_name<br />end<br />#But how do we make it vary? Pass in the name? Pull it from somewhere <br />#Lets assume there is some Griffin Family Tree in the sky<br />def test_return_full_name_is_stewie_griffin<br />assert_equal "Stewie Griffin", @person.full_name<br /> end<br />
  31. 31. For now we’ll pass in the name<br />
  32. 32. Next Test….<br />
  33. 33. Make it pass…<br />
  34. 34. WHATS THE NEXT STEP?<br />
  35. 35. The General Case<br />Integration faked by Mock object!<br />
  36. 36. MAKE IT PASS!!<br />
  37. 37. DONE!<br />

×