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