Slide 1

Slide 1 text

Swift Development
 CocoaPods, Unit Testing & BDD Presented by Sheldon Conaty 12th November 2014

Slide 2

Slide 2 text

Swift Development CocoaPods

Slide 3

Slide 3 text

- http://cocoapods.org “CocoaPods is the dependency manager for Objective-C projects. It has thousands of libraries and can help you scale your projects elegantly.” Not yet fully working for Swift: https://github.com/CocoaPods/CocoaPods/issues/2272 https://github.com/CocoaPods/CocoaPods/pull/2222

Slide 4

Slide 4 text

CocoaPod Installation • Assuming gem is installed… ! • If you need to install gem then… • Download from https://rubygems.org/pages/ download • Requires git version 1.7.5 or newer See http://guides.cocoapods.org/using/getting-started.html#getting-started

Slide 5

Slide 5 text

$ sudo gem install cocoapods Password: Fetching: i18n-0.6.11.gem (100%) Successfully installed i18n-0.6.11 Fetching: multi_json-1.10.1.gem (100%) Successfully installed multi_json-1.10.1 Fetching: activesupport-3.2.20.gem (100%) Successfully installed activesupport-3.2.20 Fetching: nap-0.8.0.gem (100%) Successfully installed nap-0.8.0 Fetching: json_pure-1.8.1.gem (100%) Successfully installed json_pure-1.8.1 Fetching: fuzzy_match-2.0.4.gem (100%) Successfully installed fuzzy_match-2.0.4 Fetching: cocoapods-core-0.34.4.gem (100%) Successfully installed cocoapods-core-0.34.4 Fetching: claide-0.7.0.gem (100%) Successfully installed claide-0.7.0 Fetching: colored-1.2.gem (100%) Successfully installed colored-1.2 Fetching: xcodeproj-0.19.4.gem (100%) Successfully installed xcodeproj-0.19.4 Fetching: cocoapods-downloader-0.7.2.gem (100%) Successfully installed cocoapods-downloader-0.7.2 Fetching: cocoapods-plugins-0.3.1.gem (100%) Successfully installed cocoapods-plugins-0.3.1 Fetching: cocoapods-try-0.4.2.gem (100%) Successfully installed cocoapods-try-0.4.2 Fetching: netrc-0.7.8.gem (100%) Successfully installed netrc-0.7.8 … … … Installing ri documentation for fuzzy_match-2.0.4 Parsing documentation for i18n-0.6.11 Installing ri documentation for i18n-0.6.11 Parsing documentation for json_pure-1.8.1 Installing ri documentation for json_pure-1.8.1 Parsing documentation for multi_json-1.10.1 Installing ri documentation for multi_json-1.10.1 Parsing documentation for nap-0.8.0 Installing ri documentation for nap-0.8.0 Parsing documentation for netrc-0.7.8 Installing ri documentation for netrc-0.7.8 Parsing documentation for open4-1.3.4 Installing ri documentation for open4-1.3.4 Parsing documentation for xcodeproj-0.19.4 Installing ri documentation for xcodeproj-0.19.4 18 gems installed

Slide 6

Slide 6 text

Podfile from http://cocoapods.org

Slide 7

Slide 7 text

Demo Using CocoaPods • Clone SwiftWeather • https://github.com/JakeLin/ SwiftWeather • Uses AFNetwork (Objective- C) via Cocoapods • pod install • bridging header • build setting • But should use Swift library • https://github.com/Alamofire/ Alamofire

Slide 8

Slide 8 text

Swift Development Debugging Hints & Tips

Slide 9

Slide 9 text

⌥ to Determine Type click on variable while pressing option key

Slide 10

Slide 10 text

Console is Your Friend Useless Useful Worth Watching: “Advanced Swift Debugging in LLDB” https://developer.apple.com/videos/wwdc/2014/#410

Slide 11

Slide 11 text

Swift Development Unit Testing (XCUnit)

Slide 12

Slide 12 text

What are Unit Tests • Automated way to test your application • Focus on smallest parts of code • Separate to integration tests • How to run tests… • “Product” -> “Test” menu • ⌘U : Run All Tests • ⌃⌥⌘U : Run Current Test
 Class

Slide 13

Slide 13 text

And Here’s One I Made Earlier… • https://github.com/sconaty/SwiftTestingTutorial • See UnitTestingExample project • Demonstrates verification of JSON parsing using: • SwiftyJSON: https://github.com/SwiftyJSON/SwiftyJSON (★2,200)

Slide 14

Slide 14 text

Common Gotchas • “unresolved identifier” error when running tests from http://natashatherobot.com/swift-unit-testing-tips-and-tricks/

Slide 15

Slide 15 text

Assertions • Unconditional Fail • Equality Test • Nil Tests • Boolean Tests • Exception Tests from Testing with Xcode: Writing Test Classes and Methods

Slide 16

Slide 16 text

Exercises • Move let json = JSON… out of each test into setUp() • Create new testThemeParagraphAlignment test. It should verify… • Initially… • [“alignment”: “right”] sets the default paragraph style to NSTextAlignment.RightTextAlignment • Continue with other possible values… • “center”: NSTextAlignment.CenterTextAlignment • “justified”: NSTextAlignment.JustifiedTextAlignment • “natural”: NSTextAlignment.NaturalTextAlignment

Slide 17

Slide 17 text

Swift Development Behaviour Driven Development

Slide 18

Slide 18 text

Problems with Unit Tests • Only on Setup() and TearDown() per XCTextCase • Can’t easily specify pending tests • Tests can only be structured by classes • Tests often need to be augmented with comments (or assertions with strings) to make purpose clear

Slide 19

Slide 19 text

Behaviour Driven Development • Swift BDD Options Available… • Quick: https://github.com/Quick/Quick (★1,483) • Sleipnir: https://github.com/railsware/Sleipnir (★506)

Slide 20

Slide 20 text

BDD & Quick • Global setup & teardown using beforeSuite and afterSuite • Define “examples” using it • Example groups using describe and context • Define group specific setup & teardown using beforeEach and afterEach • Disable Pending examples and groups • Possible to share sets of example for objects using sharedExamples and itBehavesLike

Slide 21

Slide 21 text

Adding Quick to Your Project • Using shell, add Quick as a git sub-module ! mkdir Vendor git submodule add https://github.com:Quick/Quick.git Vendor/Quick git submodule add https://github.com:Quick/Nimble.git Vendor/Nimble git submodule update --init --recursive • Add Quick.xcodeproj and Nimble.xcodeproj to test target • Link Quick.framework and Nimble.framework • Note, these are already included in the BDDExample project. So use: ! git submodule sync git submodule update --init --recursive

Slide 22

Slide 22 text

And Here’s One I Made Earlier… • https://github.com/sconaty/SwiftTestingTutorial • See BDDExample project

Slide 23

Slide 23 text

Interesting Parts of Sample • // MARK: to create sections... (line 29) • beforeEach and afterEach (lines 64 & 74) • How to read test file from bundle (line 55) • sampleJSON function and how it merges dictionaries (operator overloading) (line 85) • createThemeWithSilentLogging closure (line 16) • “alignment” group which have for loops with inner if (line 127)

Slide 24

Slide 24 text

Exercises • Create “tail indent” group (similar to “head indent”) which… • Verifies “tailIndent” double value can be read from JSON and is stored in theme.defaultParagraphStyle.tailIndent • Verifies if “tailIndent” has invalid string value in JSON then theme.defaultParagraphStyle.tailIndent defaults to 0.0 • Create “writing direction” group (similar to “"line break mode”) which… • Verifies “writingDirection” string in JSON can map string to theme.defaultParagraphStyle.baseWritingDirection. Possible mappings of JSON string value to NSWritingDirection are: • “natural”: NSWritingDirection.Natural • “leftToRight”: NSWritingDirection.LeftToRight • “rightToLeft”: NSWritingDirection.RightToLeft • Verifies if “writingDirection” has invalid string value or double in the JSON then defaults theme.defaultParagraphStyle.baseWritingDirection to .Natural

Slide 25

Slide 25 text

Useful Swift Links • Swift Reference! • Language Guide • Standard Types Reference (numeric, dictionary, array, string) • Language Grammar Reference • Apple SDKs! • AppKit Reference and UIKit Reference • BDD! • Quick & Nimble

Slide 26

Slide 26 text

Swift Testing Questions & Answers