SlideShare a Scribd company logo
Testing and ViewControllers
"Extract testable code”
&
”Test UI without the simulator”.
Small is the beauty!
• What did I do?
• Why / When / Dangers unit test
• A way to make viewControllers testable
• Make tests visible
What did I do
• TDD at Philips, small and big projects
• Lots of legacy code refactoring
• App that could have benefitted from testing
Unit testing
Integration testing
Webservice
Local device test
External web
service
VC’s Mock web service Views
UI Automation (Calabash)
Helped Developers
Helped ?
Legacy code
Legacy class
Sprout Class -> Unit tests
Unit test killers:
• Object should not set there own properties.
• Reach out to a database
• Many dependencies, no default working state on its own
Legacy code
Object should not set there own properties.
class ExampleVC: UIViewController {
let userDefaults = NSUserDefaults.standardUserDefaults()
}
class ExampleVC: UIViewController {
var userDefaults : NSUserDefaults?
override func viewDidLoad() {
super.viewDidLoad()
userDefaults = NSUserDefaults.standardUserDefaults()
}
}
Legacy code
Object should not set there own properties.
class ExampleVC: UIViewController {
lazy var userDefaults : NSUserDefaults = {
NSUserDefaults.standardUserDefaults()
}()
}
Cocoa heads testing and viewcontrollers
Why
Tests written for code coverage
are a waste of time
Write them to write better code or stop writing tests!
When
Data
Manipulation
Draw view
What value should I use for
this IndexPath?
I need a list but I have an
object with properties
ViewController
Different states
Do not test animations wait
for the result
Only visible for a very peculiar
state
What value should I use for
this IndexPath?
Data
Manipulation
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell
cell.label.text = self.datasource.item(indexPath)
return cell
}
func testThenHasMessageAndDescription() {
let resultMessage = tableDatasource.item(NSIndexPath(forItem: 0, inSection: 0))
let resultDescription = tableDatasource.item(NSIndexPath(forItem: 1, inSection: 0))
XCTAssertEqual(resultMessage!, “Message”)
XCTAssertEqual(resultDescription!, “Description”)
}
Data
Manipulation
I need a list but I have an
object with properties
let fakeResponse = ["message" : "bla", "description" : "bla some more"]
self.mappedArray = [fakeResponse["message"]!, fakeResponse["description"]!]
func item(indexPath: NSIndexPath) -> (String?){
if indexPath.item < mappedArray?.count{
return mappedArray![indexPath.item]
}else{
return nil
}
}
Draw view
Different states
Do not test animations wait
for the result
Draw view
- (void)testThenShowInPlaceError
{
XCTestExpectation *exp = [self expectationWithDescription:@"Wait for error view"];
self.viewController.viewControllerDatasource.respondWithError = YES;
[self.viewController.viewControllerDatasource reloadDataWithDataCompletion:nil
failure:nil animationCompletion:^(BOOL success) {
FBSnapshotVerifyView(self.viewController.view, @"");
[exp fulfill];
}];
NSTimeInterval animationTime = kAsyncViewsAnimationDuration;
[self waitForExpectationsWithTimeout:animationTime + 2 handler:nil];
}
Only visible for a very peculiar
state
Draw view
- (void)testThenOfferCancel
{
id vc = OCMPartialMock([[UIStoryboard storyboardWithName:@"Main" bundle:nil]
instantiateViewControllerWithIdentifier:@"ViewController"]);
//We only use a stub here to have faster test cycles
OCMStub([vc async_timeOutDelay]).andReturn(@1);
XCTestExpectation *exp = [self expectationWithDescription:@"Wait for cancel"];
[self offerCancelOnViewController:vc
exp: exp
test:^(XCTestExpectation *exp) {
FBSnapshotVerifyView(((UIViewController *) vc).view, @"");
[vc stopMocking];
[exp fulfill];
}];
}
Danger
Don’t fall in love with your tests
Make VC’s Testable
Data
Manipulation
Draw view
ViewController
SharedAsyncDatasource UIViewController+Async
Cocoa heads testing and viewcontrollers
Cocoa heads testing and viewcontrollers
Cocoa heads testing and viewcontrollers
Demo
https://github.com/doozMen/SharedAsyncViewController
Make Tests visible
Tests are just as important as application
code. Why not put them together?
Jon Reid
Inspiration
• http://qualitycoding.org/xcode-unit-testing/
• http://iosunittesting.com
• https://github.com/Inferis/IIAsyncViewController
• “Working Effectively with Legacy Code “
Michael Feathers
Ad

More Related Content

What's hot (20)

CocoaHeads Moscow. Азиз Латыпов, VIPole. «Запросы в CoreData с агрегатными фу...
CocoaHeads Moscow. Азиз Латыпов, VIPole. «Запросы в CoreData с агрегатными фу...CocoaHeads Moscow. Азиз Латыпов, VIPole. «Запросы в CoreData с агрегатными фу...
CocoaHeads Moscow. Азиз Латыпов, VIPole. «Запросы в CoreData с агрегатными фу...
Mail.ru Group
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
Divakar Gu
 
React & Redux
React & ReduxReact & Redux
React & Redux
Federico Bond
 
Tweaking the interactive grid
Tweaking the interactive gridTweaking the interactive grid
Tweaking the interactive grid
Roel Hartman
 
Developing New Widgets for your Views in Owl
Developing New Widgets for your Views in OwlDeveloping New Widgets for your Views in Owl
Developing New Widgets for your Views in Owl
Odoo
 
React redux
React reduxReact redux
React redux
Michel Perez
 
Universal adbdriverinstaller
Universal adbdriverinstallerUniversal adbdriverinstaller
Universal adbdriverinstaller
George Manrike
 
Workshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingWorkshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testing
Visual Engineering
 
Understanding backbonejs
Understanding backbonejsUnderstanding backbonejs
Understanding backbonejs
Nick Lee
 
React&redux
React&reduxReact&redux
React&redux
Blank Chen
 
Angular Performance: Then, Now and the Future. Todd Motto
Angular Performance: Then, Now and the Future. Todd MottoAngular Performance: Then, Now and the Future. Todd Motto
Angular Performance: Then, Now and the Future. Todd Motto
Future Insights
 
A Quick Introduction to YQL
A Quick Introduction to YQLA Quick Introduction to YQL
A Quick Introduction to YQL
Max Manders
 
Angular redux
Angular reduxAngular redux
Angular redux
Nir Kaufman
 
React with Redux
React with ReduxReact with Redux
React with Redux
Stanimir Todorov
 
X-Code UI testing architecture and tools
X-Code UI testing architecture and toolsX-Code UI testing architecture and tools
X-Code UI testing architecture and tools
Jianbin LIN
 
From zero to hero with the reactive extensions for java script
From zero to hero with the reactive extensions for java scriptFrom zero to hero with the reactive extensions for java script
From zero to hero with the reactive extensions for java script
Maurice De Beijer [MVP]
 
Angular js 2.0, ng poznań 20.11
Angular js 2.0, ng poznań 20.11Angular js 2.0, ng poznań 20.11
Angular js 2.0, ng poznań 20.11
Kamil Augustynowicz
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
Knoldus Inc.
 
 Mwa class custom_files
 Mwa class custom_files Mwa class custom_files
 Mwa class custom_files
Senthilkumar Shanmugam
 
Swift Delhi: Practical POP
Swift Delhi: Practical POPSwift Delhi: Practical POP
Swift Delhi: Practical POP
Natasha Murashev
 
CocoaHeads Moscow. Азиз Латыпов, VIPole. «Запросы в CoreData с агрегатными фу...
CocoaHeads Moscow. Азиз Латыпов, VIPole. «Запросы в CoreData с агрегатными фу...CocoaHeads Moscow. Азиз Латыпов, VIPole. «Запросы в CoreData с агрегатными фу...
CocoaHeads Moscow. Азиз Латыпов, VIPole. «Запросы в CoreData с агрегатными фу...
Mail.ru Group
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
Divakar Gu
 
Tweaking the interactive grid
Tweaking the interactive gridTweaking the interactive grid
Tweaking the interactive grid
Roel Hartman
 
Developing New Widgets for your Views in Owl
Developing New Widgets for your Views in OwlDeveloping New Widgets for your Views in Owl
Developing New Widgets for your Views in Owl
Odoo
 
Universal adbdriverinstaller
Universal adbdriverinstallerUniversal adbdriverinstaller
Universal adbdriverinstaller
George Manrike
 
Workshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingWorkshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testing
Visual Engineering
 
Understanding backbonejs
Understanding backbonejsUnderstanding backbonejs
Understanding backbonejs
Nick Lee
 
Angular Performance: Then, Now and the Future. Todd Motto
Angular Performance: Then, Now and the Future. Todd MottoAngular Performance: Then, Now and the Future. Todd Motto
Angular Performance: Then, Now and the Future. Todd Motto
Future Insights
 
A Quick Introduction to YQL
A Quick Introduction to YQLA Quick Introduction to YQL
A Quick Introduction to YQL
Max Manders
 
X-Code UI testing architecture and tools
X-Code UI testing architecture and toolsX-Code UI testing architecture and tools
X-Code UI testing architecture and tools
Jianbin LIN
 
From zero to hero with the reactive extensions for java script
From zero to hero with the reactive extensions for java scriptFrom zero to hero with the reactive extensions for java script
From zero to hero with the reactive extensions for java script
Maurice De Beijer [MVP]
 
Angular js 2.0, ng poznań 20.11
Angular js 2.0, ng poznań 20.11Angular js 2.0, ng poznań 20.11
Angular js 2.0, ng poznań 20.11
Kamil Augustynowicz
 
Swift Delhi: Practical POP
Swift Delhi: Practical POPSwift Delhi: Practical POP
Swift Delhi: Practical POP
Natasha Murashev
 

Similar to Cocoa heads testing and viewcontrollers (20)

TDD by Controlling Dependencies
TDD by Controlling DependenciesTDD by Controlling Dependencies
TDD by Controlling Dependencies
Jorge Ortiz
 
Testing the waters of iOS
Testing the waters of iOSTesting the waters of iOS
Testing the waters of iOS
Kremizas Kostas
 
Swift testing ftw
Swift testing ftwSwift testing ftw
Swift testing ftw
Jorge Ortiz
 
Unit Testing: Special Cases
Unit Testing: Special CasesUnit Testing: Special Cases
Unit Testing: Special Cases
Ciklum Ukraine
 
Functional Core, Reactive Shell
Functional Core, Reactive ShellFunctional Core, Reactive Shell
Functional Core, Reactive Shell
Giovanni Lodi
 
Snapshot Testing @ CocoaheadsNL
Snapshot Testing @ CocoaheadsNLSnapshot Testing @ CocoaheadsNL
Snapshot Testing @ CocoaheadsNL
Lars Lockefeer
 
20180721 code defragment
20180721 code defragment20180721 code defragment
20180721 code defragment
Chiwon Song
 
Everything You (N)ever Wanted to Know about Testing View Controllers
Everything You (N)ever Wanted to Know about Testing View ControllersEverything You (N)ever Wanted to Know about Testing View Controllers
Everything You (N)ever Wanted to Know about Testing View Controllers
Brian Gesiak
 
[XCode] Automating UI Testing
[XCode] Automating UI Testing[XCode] Automating UI Testing
[XCode] Automating UI Testing
Phineas Huang
 
Swift - One step forward from Obj-C
Swift -  One step forward from Obj-CSwift -  One step forward from Obj-C
Swift - One step forward from Obj-C
Nissan Tsafrir
 
Writing Your App Swiftly
Writing Your App SwiftlyWriting Your App Swiftly
Writing Your App Swiftly
Sommer Panage
 
Unit testing UIView
Unit testing UIViewUnit testing UIView
Unit testing UIView
Pierre Felgines
 
Test or Go Fishing - a guide on how to write better Swift for iOS
Test or Go Fishing - a guide on how to write better Swift for iOSTest or Go Fishing - a guide on how to write better Swift for iOS
Test or Go Fishing - a guide on how to write better Swift for iOS
Paul Ardeleanu
 
French kit2019
French kit2019French kit2019
French kit2019
Nataliya Patsovska
 
Test or Go Fishing - a guide on how to write better Swift for iOS
Test or Go Fishing - a guide on how to write better Swift for iOSTest or Go Fishing - a guide on how to write better Swift for iOS
Test or Go Fishing - a guide on how to write better Swift for iOS
Paul Ardeleanu
 
Automatic reference counting (arc) and memory management in swift
Automatic reference counting (arc) and memory management in swiftAutomatic reference counting (arc) and memory management in swift
Automatic reference counting (arc) and memory management in swift
InnovationM
 
Think sharp, write swift
Think sharp, write swiftThink sharp, write swift
Think sharp, write swift
Pascal Batty
 
Connect.Tech- Swift Memory Management
Connect.Tech- Swift Memory ManagementConnect.Tech- Swift Memory Management
Connect.Tech- Swift Memory Management
stable|kernel
 
Quick: Better Tests via Incremental Setup
Quick: Better Tests via Incremental SetupQuick: Better Tests via Incremental Setup
Quick: Better Tests via Incremental Setup
Brian Gesiak
 
Swift Tableview iOS App Development
Swift Tableview iOS App DevelopmentSwift Tableview iOS App Development
Swift Tableview iOS App Development
Ketan Raval
 
TDD by Controlling Dependencies
TDD by Controlling DependenciesTDD by Controlling Dependencies
TDD by Controlling Dependencies
Jorge Ortiz
 
Testing the waters of iOS
Testing the waters of iOSTesting the waters of iOS
Testing the waters of iOS
Kremizas Kostas
 
Swift testing ftw
Swift testing ftwSwift testing ftw
Swift testing ftw
Jorge Ortiz
 
Unit Testing: Special Cases
Unit Testing: Special CasesUnit Testing: Special Cases
Unit Testing: Special Cases
Ciklum Ukraine
 
Functional Core, Reactive Shell
Functional Core, Reactive ShellFunctional Core, Reactive Shell
Functional Core, Reactive Shell
Giovanni Lodi
 
Snapshot Testing @ CocoaheadsNL
Snapshot Testing @ CocoaheadsNLSnapshot Testing @ CocoaheadsNL
Snapshot Testing @ CocoaheadsNL
Lars Lockefeer
 
20180721 code defragment
20180721 code defragment20180721 code defragment
20180721 code defragment
Chiwon Song
 
Everything You (N)ever Wanted to Know about Testing View Controllers
Everything You (N)ever Wanted to Know about Testing View ControllersEverything You (N)ever Wanted to Know about Testing View Controllers
Everything You (N)ever Wanted to Know about Testing View Controllers
Brian Gesiak
 
[XCode] Automating UI Testing
[XCode] Automating UI Testing[XCode] Automating UI Testing
[XCode] Automating UI Testing
Phineas Huang
 
Swift - One step forward from Obj-C
Swift -  One step forward from Obj-CSwift -  One step forward from Obj-C
Swift - One step forward from Obj-C
Nissan Tsafrir
 
Writing Your App Swiftly
Writing Your App SwiftlyWriting Your App Swiftly
Writing Your App Swiftly
Sommer Panage
 
Test or Go Fishing - a guide on how to write better Swift for iOS
Test or Go Fishing - a guide on how to write better Swift for iOSTest or Go Fishing - a guide on how to write better Swift for iOS
Test or Go Fishing - a guide on how to write better Swift for iOS
Paul Ardeleanu
 
Test or Go Fishing - a guide on how to write better Swift for iOS
Test or Go Fishing - a guide on how to write better Swift for iOSTest or Go Fishing - a guide on how to write better Swift for iOS
Test or Go Fishing - a guide on how to write better Swift for iOS
Paul Ardeleanu
 
Automatic reference counting (arc) and memory management in swift
Automatic reference counting (arc) and memory management in swiftAutomatic reference counting (arc) and memory management in swift
Automatic reference counting (arc) and memory management in swift
InnovationM
 
Think sharp, write swift
Think sharp, write swiftThink sharp, write swift
Think sharp, write swift
Pascal Batty
 
Connect.Tech- Swift Memory Management
Connect.Tech- Swift Memory ManagementConnect.Tech- Swift Memory Management
Connect.Tech- Swift Memory Management
stable|kernel
 
Quick: Better Tests via Incremental Setup
Quick: Better Tests via Incremental SetupQuick: Better Tests via Incremental Setup
Quick: Better Tests via Incremental Setup
Brian Gesiak
 
Swift Tableview iOS App Development
Swift Tableview iOS App DevelopmentSwift Tableview iOS App Development
Swift Tableview iOS App Development
Ketan Raval
 
Ad

Recently uploaded (20)

A Study of Bank Line Shifting of the Selected Reach of Jamuna River Using Mul...
A Study of Bank Line Shifting of the Selected Reach of Jamuna River Using Mul...A Study of Bank Line Shifting of the Selected Reach of Jamuna River Using Mul...
A Study of Bank Line Shifting of the Selected Reach of Jamuna River Using Mul...
Journal of Soft Computing in Civil Engineering
 
AI-Powered Data Management and Governance in Retail
AI-Powered Data Management and Governance in RetailAI-Powered Data Management and Governance in Retail
AI-Powered Data Management and Governance in Retail
IJDKP
 
UNIT 3 Software Engineering (BCS601) EIOV.pdf
UNIT 3 Software Engineering (BCS601) EIOV.pdfUNIT 3 Software Engineering (BCS601) EIOV.pdf
UNIT 3 Software Engineering (BCS601) EIOV.pdf
sikarwaramit089
 
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation RateModeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Journal of Soft Computing in Civil Engineering
 
introduction to Rapid Tooling and Additive Manufacturing Applications
introduction to Rapid Tooling and Additive Manufacturing Applicationsintroduction to Rapid Tooling and Additive Manufacturing Applications
introduction to Rapid Tooling and Additive Manufacturing Applications
vijimech408
 
vtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdfvtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdf
RaghavaGD1
 
698642933-DdocfordownloadEEP-FAKE-PPT.pptx
698642933-DdocfordownloadEEP-FAKE-PPT.pptx698642933-DdocfordownloadEEP-FAKE-PPT.pptx
698642933-DdocfordownloadEEP-FAKE-PPT.pptx
speedcomcyber25
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
Guru Nanak Technical Institutions
 
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptxUnleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
SanjeetMishra29
 
David Boutry - Specializes In AWS, Microservices And Python
David Boutry - Specializes In AWS, Microservices And PythonDavid Boutry - Specializes In AWS, Microservices And Python
David Boutry - Specializes In AWS, Microservices And Python
David Boutry
 
Espresso PD Official MP_eng Version.pptx
Espresso PD Official MP_eng Version.pptxEspresso PD Official MP_eng Version.pptx
Espresso PD Official MP_eng Version.pptx
NingChacha1
 
PYTHON--QUIZ-1_20250422_002514_0000.pptx
PYTHON--QUIZ-1_20250422_002514_0000.pptxPYTHON--QUIZ-1_20250422_002514_0000.pptx
PYTHON--QUIZ-1_20250422_002514_0000.pptx
rmvigram
 
UNIT 5 Software Engineering sem 6 EIOV.pdf
UNIT 5  Software Engineering sem 6 EIOV.pdfUNIT 5  Software Engineering sem 6 EIOV.pdf
UNIT 5 Software Engineering sem 6 EIOV.pdf
sikarwaramit089
 
ldr darkness sensor circuit.pptx for engineers
ldr darkness sensor circuit.pptx for engineersldr darkness sensor circuit.pptx for engineers
ldr darkness sensor circuit.pptx for engineers
PravalikaChidurala
 
May 2025 - Top 10 Read Articles in Network Security and Its Applications
May 2025 - Top 10 Read Articles in Network Security and Its ApplicationsMay 2025 - Top 10 Read Articles in Network Security and Its Applications
May 2025 - Top 10 Read Articles in Network Security and Its Applications
IJNSA Journal
 
Understand water laser communication using Arduino laser and solar panel
Understand water laser communication using Arduino laser and solar panelUnderstand water laser communication using Arduino laser and solar panel
Understand water laser communication using Arduino laser and solar panel
NaveenBotsa
 
Python Functions, Modules and Packages
Python Functions, Modules and PackagesPython Functions, Modules and Packages
Python Functions, Modules and Packages
Dr. A. B. Shinde
 
Zeiss-Ultra-Optimeter metrology subject.pdf
Zeiss-Ultra-Optimeter metrology subject.pdfZeiss-Ultra-Optimeter metrology subject.pdf
Zeiss-Ultra-Optimeter metrology subject.pdf
Saikumar174642
 
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
Jimmy Lai
 
AI-Powered Data Management and Governance in Retail
AI-Powered Data Management and Governance in RetailAI-Powered Data Management and Governance in Retail
AI-Powered Data Management and Governance in Retail
IJDKP
 
UNIT 3 Software Engineering (BCS601) EIOV.pdf
UNIT 3 Software Engineering (BCS601) EIOV.pdfUNIT 3 Software Engineering (BCS601) EIOV.pdf
UNIT 3 Software Engineering (BCS601) EIOV.pdf
sikarwaramit089
 
introduction to Rapid Tooling and Additive Manufacturing Applications
introduction to Rapid Tooling and Additive Manufacturing Applicationsintroduction to Rapid Tooling and Additive Manufacturing Applications
introduction to Rapid Tooling and Additive Manufacturing Applications
vijimech408
 
vtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdfvtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdf
RaghavaGD1
 
698642933-DdocfordownloadEEP-FAKE-PPT.pptx
698642933-DdocfordownloadEEP-FAKE-PPT.pptx698642933-DdocfordownloadEEP-FAKE-PPT.pptx
698642933-DdocfordownloadEEP-FAKE-PPT.pptx
speedcomcyber25
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptxUnleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
SanjeetMishra29
 
David Boutry - Specializes In AWS, Microservices And Python
David Boutry - Specializes In AWS, Microservices And PythonDavid Boutry - Specializes In AWS, Microservices And Python
David Boutry - Specializes In AWS, Microservices And Python
David Boutry
 
Espresso PD Official MP_eng Version.pptx
Espresso PD Official MP_eng Version.pptxEspresso PD Official MP_eng Version.pptx
Espresso PD Official MP_eng Version.pptx
NingChacha1
 
PYTHON--QUIZ-1_20250422_002514_0000.pptx
PYTHON--QUIZ-1_20250422_002514_0000.pptxPYTHON--QUIZ-1_20250422_002514_0000.pptx
PYTHON--QUIZ-1_20250422_002514_0000.pptx
rmvigram
 
UNIT 5 Software Engineering sem 6 EIOV.pdf
UNIT 5  Software Engineering sem 6 EIOV.pdfUNIT 5  Software Engineering sem 6 EIOV.pdf
UNIT 5 Software Engineering sem 6 EIOV.pdf
sikarwaramit089
 
ldr darkness sensor circuit.pptx for engineers
ldr darkness sensor circuit.pptx for engineersldr darkness sensor circuit.pptx for engineers
ldr darkness sensor circuit.pptx for engineers
PravalikaChidurala
 
May 2025 - Top 10 Read Articles in Network Security and Its Applications
May 2025 - Top 10 Read Articles in Network Security and Its ApplicationsMay 2025 - Top 10 Read Articles in Network Security and Its Applications
May 2025 - Top 10 Read Articles in Network Security and Its Applications
IJNSA Journal
 
Understand water laser communication using Arduino laser and solar panel
Understand water laser communication using Arduino laser and solar panelUnderstand water laser communication using Arduino laser and solar panel
Understand water laser communication using Arduino laser and solar panel
NaveenBotsa
 
Python Functions, Modules and Packages
Python Functions, Modules and PackagesPython Functions, Modules and Packages
Python Functions, Modules and Packages
Dr. A. B. Shinde
 
Zeiss-Ultra-Optimeter metrology subject.pdf
Zeiss-Ultra-Optimeter metrology subject.pdfZeiss-Ultra-Optimeter metrology subject.pdf
Zeiss-Ultra-Optimeter metrology subject.pdf
Saikumar174642
 
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
Jimmy Lai
 
Ad

Cocoa heads testing and viewcontrollers

  • 1. Testing and ViewControllers "Extract testable code” & ”Test UI without the simulator”. Small is the beauty!
  • 2. • What did I do? • Why / When / Dangers unit test • A way to make viewControllers testable • Make tests visible
  • 3. What did I do • TDD at Philips, small and big projects • Lots of legacy code refactoring • App that could have benefitted from testing
  • 4. Unit testing Integration testing Webservice Local device test External web service VC’s Mock web service Views UI Automation (Calabash) Helped Developers Helped ?
  • 5. Legacy code Legacy class Sprout Class -> Unit tests Unit test killers: • Object should not set there own properties. • Reach out to a database • Many dependencies, no default working state on its own
  • 6. Legacy code Object should not set there own properties. class ExampleVC: UIViewController { let userDefaults = NSUserDefaults.standardUserDefaults() } class ExampleVC: UIViewController { var userDefaults : NSUserDefaults? override func viewDidLoad() { super.viewDidLoad() userDefaults = NSUserDefaults.standardUserDefaults() } }
  • 7. Legacy code Object should not set there own properties. class ExampleVC: UIViewController { lazy var userDefaults : NSUserDefaults = { NSUserDefaults.standardUserDefaults() }() }
  • 9. Why Tests written for code coverage are a waste of time Write them to write better code or stop writing tests!
  • 10. When Data Manipulation Draw view What value should I use for this IndexPath? I need a list but I have an object with properties ViewController Different states Do not test animations wait for the result Only visible for a very peculiar state
  • 11. What value should I use for this IndexPath? Data Manipulation override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell cell.label.text = self.datasource.item(indexPath) return cell } func testThenHasMessageAndDescription() { let resultMessage = tableDatasource.item(NSIndexPath(forItem: 0, inSection: 0)) let resultDescription = tableDatasource.item(NSIndexPath(forItem: 1, inSection: 0)) XCTAssertEqual(resultMessage!, “Message”) XCTAssertEqual(resultDescription!, “Description”) }
  • 12. Data Manipulation I need a list but I have an object with properties let fakeResponse = ["message" : "bla", "description" : "bla some more"] self.mappedArray = [fakeResponse["message"]!, fakeResponse["description"]!] func item(indexPath: NSIndexPath) -> (String?){ if indexPath.item < mappedArray?.count{ return mappedArray![indexPath.item] }else{ return nil } }
  • 14. Do not test animations wait for the result Draw view - (void)testThenShowInPlaceError { XCTestExpectation *exp = [self expectationWithDescription:@"Wait for error view"]; self.viewController.viewControllerDatasource.respondWithError = YES; [self.viewController.viewControllerDatasource reloadDataWithDataCompletion:nil failure:nil animationCompletion:^(BOOL success) { FBSnapshotVerifyView(self.viewController.view, @""); [exp fulfill]; }]; NSTimeInterval animationTime = kAsyncViewsAnimationDuration; [self waitForExpectationsWithTimeout:animationTime + 2 handler:nil]; }
  • 15. Only visible for a very peculiar state Draw view - (void)testThenOfferCancel { id vc = OCMPartialMock([[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"]); //We only use a stub here to have faster test cycles OCMStub([vc async_timeOutDelay]).andReturn(@1); XCTestExpectation *exp = [self expectationWithDescription:@"Wait for cancel"]; [self offerCancelOnViewController:vc exp: exp test:^(XCTestExpectation *exp) { FBSnapshotVerifyView(((UIViewController *) vc).view, @""); [vc stopMocking]; [exp fulfill]; }]; }
  • 16. Danger Don’t fall in love with your tests
  • 17. Make VC’s Testable Data Manipulation Draw view ViewController SharedAsyncDatasource UIViewController+Async
  • 22. Make Tests visible Tests are just as important as application code. Why not put them together? Jon Reid
  • 23. Inspiration • http://qualitycoding.org/xcode-unit-testing/ • http://iosunittesting.com • https://github.com/Inferis/IIAsyncViewController • “Working Effectively with Legacy Code “ Michael Feathers