intro to test driven development for ios - quality coding · costs and benefits of tdd costs: •...
TRANSCRIPT
![Page 1: Intro to Test Driven Development for iOS - Quality Coding · Costs and Benefits of TDD Costs: • 15–35% increase in “initial development time” • Plus slowdown while learning](https://reader034.vdocuments.mx/reader034/viewer/2022052612/5f09b6a77e708231d428293a/html5/thumbnails/1.jpg)
Jon Reid http://qualitycoding.org/tdd-workshop/
Intro toTest Driven Development
for iOS
http://www.flickr.com/photos/stefan-w/3337072853
![Page 2: Intro to Test Driven Development for iOS - Quality Coding · Costs and Benefits of TDD Costs: • 15–35% increase in “initial development time” • Plus slowdown while learning](https://reader034.vdocuments.mx/reader034/viewer/2022052612/5f09b6a77e708231d428293a/html5/thumbnails/2.jpg)
What Are Some Qualities of Clean Code?
•
•
•
•
•
•
•
![Page 3: Intro to Test Driven Development for iOS - Quality Coding · Costs and Benefits of TDD Costs: • 15–35% increase in “initial development time” • Plus slowdown while learning](https://reader034.vdocuments.mx/reader034/viewer/2022052612/5f09b6a77e708231d428293a/html5/thumbnails/3.jpg)
Write a failing test
Make the test pass
Refactor
The 3 Steps
![Page 4: Intro to Test Driven Development for iOS - Quality Coding · Costs and Benefits of TDD Costs: • 15–35% increase in “initial development time” • Plus slowdown while learning](https://reader034.vdocuments.mx/reader034/viewer/2022052612/5f09b6a77e708231d428293a/html5/thumbnails/4.jpg)
Uncle Bob’s 3 Laws of TDD1. Write no production
code, except to pass a failing test.
2. Write only enough of a test to demonstrate a failure.(Compiler errors are failures.)
3. Write only enough production code to pass a test.
![Page 5: Intro to Test Driven Development for iOS - Quality Coding · Costs and Benefits of TDD Costs: • 15–35% increase in “initial development time” • Plus slowdown while learning](https://reader034.vdocuments.mx/reader034/viewer/2022052612/5f09b6a77e708231d428293a/html5/thumbnails/5.jpg)
Tools I Like to Use• AppCode
If you use Xcode 8.2: https://www.jetbrains.com/objc/download/ If you use Xcode 8.3: https://confluence.jetbrains.com/display/OBJC/AppCode+EAP
• Custom Test Templates http://qualitycoding.org/files/XCTest-Templates.zip
• Custom Code Snippets http://qualitycoding.org/files/Snippets.zip
![Page 6: Intro to Test Driven Development for iOS - Quality Coding · Costs and Benefits of TDD Costs: • 15–35% increase in “initial development time” • Plus slowdown while learning](https://reader034.vdocuments.mx/reader034/viewer/2022052612/5f09b6a77e708231d428293a/html5/thumbnails/6.jpg)
Fizz BuzzCode Kata
• Create a function that will return “Fizz” if its argument is a multiple of 3.
• The function should return “Buzz” if the argument is a multiple of 5.
• If the argument is a multiple of both 3 and 5, it should return “FizzBuzz”.
• Otherwise, it should return the argument in string form.
![Page 7: Intro to Test Driven Development for iOS - Quality Coding · Costs and Benefits of TDD Costs: • 15–35% increase in “initial development time” • Plus slowdown while learning](https://reader034.vdocuments.mx/reader034/viewer/2022052612/5f09b6a77e708231d428293a/html5/thumbnails/7.jpg)
Bowling Game Code Kata
Write a class that has two methods:
• roll(_ pins: Int) is called each time the player rolls a ball. The argument is the number of pins knocked down.
• score() -> Int is called only at the very end of the game. It returns the total score for that game.
Objective-C and Swift walkthroughs: http://qualitycoding.org/xcode-tdd/
![Page 8: Intro to Test Driven Development for iOS - Quality Coding · Costs and Benefits of TDD Costs: • 15–35% increase in “initial development time” • Plus slowdown while learning](https://reader034.vdocuments.mx/reader034/viewer/2022052612/5f09b6a77e708231d428293a/html5/thumbnails/8.jpg)
Let’s TDD a View Controller!
1. Increment and decrement a counter (see “View Controller Tricks”)
2. Add persistence via UserDefaults(see “Test Doubles in Swift”)
![Page 9: Intro to Test Driven Development for iOS - Quality Coding · Costs and Benefits of TDD Costs: • 15–35% increase in “initial development time” • Plus slowdown while learning](https://reader034.vdocuments.mx/reader034/viewer/2022052612/5f09b6a77e708231d428293a/html5/thumbnails/9.jpg)
View Controller Tricks• How to load view controller if you use Storyboards:
let storyboard = UIStoryboard(name: "WHAT", bundle: nil)let sut = storyboard.instantiateViewController( withIdentifier: "SOMETHING") as! MyViewController
• Force any view controller to load its view: _ = sut.view
• Simulate “touch up inside” action on button: sut.button.sendActions(for: .touchUpInside)[sut.button sendActionsForControlEvents:UIControlEventTouchUpInside];
![Page 10: Intro to Test Driven Development for iOS - Quality Coding · Costs and Benefits of TDD Costs: • 15–35% increase in “initial development time” • Plus slowdown while learning](https://reader034.vdocuments.mx/reader034/viewer/2022052612/5f09b6a77e708231d428293a/html5/thumbnails/10.jpg)
Test Doubles in Swift• Declare a protocol
• Attach to actual class with extension
• Production code: use protocol, not actual class
• Test code: supply test double that conforms to protocol
https://realm.io/news/making-mock-objects-more-useful-try-swift-2017/
![Page 11: Intro to Test Driven Development for iOS - Quality Coding · Costs and Benefits of TDD Costs: • 15–35% increase in “initial development time” • Plus slowdown while learning](https://reader034.vdocuments.mx/reader034/viewer/2022052612/5f09b6a77e708231d428293a/html5/thumbnails/11.jpg)
Dependency Injection• Favor “Tell, Don’t Ask”
• Pass “stuff it needs” in initializer if possible. …Can use default parameters for “actual objects”
• If initializer isn’t possible, pass via properties. …Can use default values for “actual objects”
• Remember, you can also inject blocks for deferred calls
![Page 12: Intro to Test Driven Development for iOS - Quality Coding · Costs and Benefits of TDD Costs: • 15–35% increase in “initial development time” • Plus slowdown while learning](https://reader034.vdocuments.mx/reader034/viewer/2022052612/5f09b6a77e708231d428293a/html5/thumbnails/12.jpg)
![Page 13: Intro to Test Driven Development for iOS - Quality Coding · Costs and Benefits of TDD Costs: • 15–35% increase in “initial development time” • Plus slowdown while learning](https://reader034.vdocuments.mx/reader034/viewer/2022052612/5f09b6a77e708231d428293a/html5/thumbnails/13.jpg)
Costs and Benefits of TDD Costs:
• 15–35% increase in “initial development time”
• Plus slowdown while learning any new discipline
Initial Benefits:
• 40-90% decrease in defect density
• Fewer defects means lower debugging costs (people × time). Should result in nearly same “time to ship”
Ongoing Benefits:
• Code is cleaner due to testability requirement: reduced coupling
• Fully covered code can be safely refactored: agility increase
![Page 14: Intro to Test Driven Development for iOS - Quality Coding · Costs and Benefits of TDD Costs: • 15–35% increase in “initial development time” • Plus slowdown while learning](https://reader034.vdocuments.mx/reader034/viewer/2022052612/5f09b6a77e708231d428293a/html5/thumbnails/14.jpg)
TDD Level: Easy• Model objects
• Business rules
• Create network request
• Process network response
TDD Level: Advanced• View navigation
• Networking
• Filesystem / Database
• Analytics
TDD Level: Intermediate• View controllers
• Invoke user actions
• Table views
• NSNotifications
TDD Level: Avoid• Actual networking
• Actual filesystem
• Actual database
• Asynchronous calls