15
Jon Reid http://qualitycoding.org/tdd-workshop/ Intro to Test Driven Development for iOS http://www.flickr.com/photos/stefan-w/3337072853

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

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Page 15: 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

http://qualitycoding.org/tdd-workshop/