SlideShare a Scribd company logo
Testable apps
in Swift
Andrzej Dąbrowski andrzej@amplituda.io amplituda.io
A client comes…
– Our Beloved Client
“I have a start-up, I need an app, previous team
sucked so I’d like you to continue develop the
project”
Great!
Let’s look at the code.
1Split code to independent 

single-purpose services.
(no tests without that, sorry)
2Write couple of tests.
Example service:
BeaconManager
Public API
Why should I make a
wrapper around
CLLocationManager?
• seamlessly manages permissions
• smoothens out beacon power fluctuations
• doesn’t require all listeners to duplicate single beacon
filtering code and leaving beacon logic
• enable / disable listening
• saves energy (takes care of switching between
monitoring/ranging automatically)
• takes care of background ranging (☠☠☠)
• it’s listeners actually don’t have to care about
anything, only implement code on beacon events.
BeaconManager uses
another services/APIs
• CLLocationManager for permissions and
background ranging (updating location)
• Logger (sends critical logs to back-end)
• Data
• SyncManager
• NSNotificationCenter
[CocoaHeads Tricity] Andrzej Dąbrowski - Dependency injection and testable architecture in Swift apps
BeaconManager is
critical service to test
Singletons.
Hard to test.
Objective-C dynamic runtime allows us
easily change implementation of classes /
methods.
OCMock!
Can I do this in Swift?
Hmm…
Let’s read
http://ocmock.org/swift/
There are smart people around who
claim differently:
http://nshipster.com/swift-objc-
runtime/
IMVHO, not the best option.
Where is Swift going?
Will there ever be runtime access
in Swift?
I have no idea.
But if Swift is about to be
super-fast and super-safe,
I wouldn’t count on it.
Why Obj-C runtime is so
great in testing?
• Effortless and quick mocks/stubs
• No refactoring needed at all. Just jump into
writing tests.
Disadvantages of OCMock
way of testing?
• No compile-time protection
• We can forget to stub some important things
• Encourages a bit to write ugly code (inline mocks /
stubs) which could create problems with duplication
later
• No explicit list of dependencies like in dependency
injection
• Still have to write mocks / stubs :(
Alternatives?
Stubbing based on abstraction
layer.
Service Locator or DI
How does it work?
Service Locator
[CocoaHeads Tricity] Andrzej Dąbrowski - Dependency injection and testable architecture in Swift apps
[CocoaHeads Tricity] Andrzej Dąbrowski - Dependency injection and testable architecture in Swift apps
[CocoaHeads Tricity] Andrzej Dąbrowski - Dependency injection and testable architecture in Swift apps
Advantages of SL
• compile-time
• quite quick to implement (actually,
sharedInstance properties could be overwritten
so that they call Service Locator themselves).
• Service protocols are a beautiful way to
investigate app architecture without going too
deep into implementation details
Disadvantages of SL
• We need to complicate architecture (is that really
bad thing?)
• We can forget to stub some libs
• No explicit list of dependencies (like in DI)
• Still have to write our stubs :(
Dependency injection
"Dependency Injection" is a 25-
dollar term for a 5-cent concept.
James Shore
"Dependency Injection" is a 25-dollar
term for a 5-cent concept.
James Shore
[CocoaHeads Tricity] Andrzej Dąbrowski - Dependency injection and testable architecture in Swift apps
[CocoaHeads Tricity] Andrzej Dąbrowski - Dependency injection and testable architecture in Swift apps
[CocoaHeads Tricity] Andrzej Dąbrowski - Dependency injection and testable architecture in Swift apps
We must initialise services
and inject properties when
app starts.
We could use some
automation for initialising
services.
Advantages of DI
• compile-time
• explicit list of dependencies (nice app
architecture guard)
• we won’t forget to stub anything
• service protocols are a beautiful way to
investigate app architecture without going too
deep into implementation details
Disadvantages of DI
• Quite not-so-fast to implement
• Architecture gets more complicated (like in SL)
• We still need to write stubs
SL vs. DI
• http://www.martinfowler.com/articles/
injection.html
• http://bayou.io/draft/
In_Defense_of_Service_Locator.html
• rest of the Internet
Are there any libraries for
Dependency Injection?
Typhoon
Swinject
Typhoon
December, 2012 1667
StarsFirst github commit
Swinject
July, 2015 454
StarsFirst github commit
There’s a single
disadvantage of all testing
methods.
No lib with stubs.
I’d like to test like this
Why do I have to write
mocks/stubs for Apple or
3rd party libs?
Let’s take a look at
modern framework like
angular.js
Which iOS frameworks
could be easily stubbed
not to duplicate code?
Summary
• method / class swizzling is probably not what you want
in Swift
• service locator is quickest way to get legacy code
tested
• typhoon and swinject are 3rd party libs for Swift testing
• swinject seems to be nice
• unfortunately, there are no libraries which out of the
box give developers stubs / mocks for Apple APIs.
Thank you

More Related Content

PDF
RubyConf Taiwan 2016 - Large scale Rails applications
PDF
RubyConf China 2015 - Rails off assets pipeline
PDF
EmberCLI Rails: An Integration Love Story.
PDF
Rails api + JS app
PDF
CI/CD and Asset Serving for Single Page Apps
PDF
Developing and Deploying Deep Learning Based Computer Vision Systems - Alka N...
PDF
Angular 2 observables
PPTX
Continous integration and delivery for single page applications
RubyConf Taiwan 2016 - Large scale Rails applications
RubyConf China 2015 - Rails off assets pipeline
EmberCLI Rails: An Integration Love Story.
Rails api + JS app
CI/CD and Asset Serving for Single Page Apps
Developing and Deploying Deep Learning Based Computer Vision Systems - Alka N...
Angular 2 observables
Continous integration and delivery for single page applications

What's hot (20)

PPT
Streamlined Geek Talk
PDF
Micro Services - Small is Beautiful
PPTX
IaC? VSTS to the rescue! Abbreviations explained
PDF
Automate REST API Testing
PDF
"Frameworks in 2015" Андрей Листочкин
PDF
AWS Lambda Function with Kotlin
PDF
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
PPTX
Web api using rest based architecture
PDF
Algolia's Fury Road to a Worldwide API
PDF
Serverless Application Model - Executing Lambdas Locally
PPTX
Life Beyond Rails: Creating Cross Platform Ruby Apps
PDF
Alfresco tech talk live mobile sdks
PDF
Android Clean Architecture for Dummies
PPTX
Test automation architecture. Advanced level
PDF
Spring Boot
PDF
Micro Service – The New Architecture Paradigm
PPTX
Angular 2 + TypeScript = true. Let's Play!
PDF
Integration Testing with Docker Containers with DockerCompose
PDF
Legacy Sins
PDF
Phoenix for Rubyists
Streamlined Geek Talk
Micro Services - Small is Beautiful
IaC? VSTS to the rescue! Abbreviations explained
Automate REST API Testing
"Frameworks in 2015" Андрей Листочкин
AWS Lambda Function with Kotlin
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
Web api using rest based architecture
Algolia's Fury Road to a Worldwide API
Serverless Application Model - Executing Lambdas Locally
Life Beyond Rails: Creating Cross Platform Ruby Apps
Alfresco tech talk live mobile sdks
Android Clean Architecture for Dummies
Test automation architecture. Advanced level
Spring Boot
Micro Service – The New Architecture Paradigm
Angular 2 + TypeScript = true. Let's Play!
Integration Testing with Docker Containers with DockerCompose
Legacy Sins
Phoenix for Rubyists
Ad

Viewers also liked (16)

PPT
Old Meets New
PPTX
Galaxy bags
PDF
Juan fernando gonzales gerardo ramirez zuluaga 8 a
PDF
Konsultacje społeczne (1)
PDF
DOCX
letter
PDF
تاريخ بيت المقدس لابن الجوزي
PPTX
بدع الموت والتعزية
PPTX
The Selection of Apps for Young Learners - Considerations, Criteria, and Exem...
PPTX
La gestión de Imagen como factor de posicionamiento
PDF
SHCR Review 2015 - Appendix 1 Literature Review
PDF
Proposal Masjid Baitul Ummah Oma Batam Center
PDF
Proposal Pembangunan Masjid Jami Al Hidayah
PPTX
Rebecca Grant - DH research data: identification and challenges (DH2016)
PDF
Time Inc. Books: Cooking Verticals
PDF
إدمان الجنس والإباحية
Old Meets New
Galaxy bags
Juan fernando gonzales gerardo ramirez zuluaga 8 a
Konsultacje społeczne (1)
letter
تاريخ بيت المقدس لابن الجوزي
بدع الموت والتعزية
The Selection of Apps for Young Learners - Considerations, Criteria, and Exem...
La gestión de Imagen como factor de posicionamiento
SHCR Review 2015 - Appendix 1 Literature Review
Proposal Masjid Baitul Ummah Oma Batam Center
Proposal Pembangunan Masjid Jami Al Hidayah
Rebecca Grant - DH research data: identification and challenges (DH2016)
Time Inc. Books: Cooking Verticals
إدمان الجنس والإباحية
Ad

Similar to [CocoaHeads Tricity] Andrzej Dąbrowski - Dependency injection and testable architecture in Swift apps (20)

PDF
Fed London - January 2015
PDF
Patterns and practices for building enterprise-scale HTML5 apps
PPTX
Reactive Micro Services with Java seminar
PDF
How to Build Front-End Web Apps that Scale - FutureJS
PDF
AngularJSTO presentation
PDF
Building front-end apps that Scale - FOSDEM 2014
PDF
AWS Lambda from the trenches (Serverless London)
PDF
Serverless in production, an experience report (BuildStuff)
PDF
Microservices: The Best Practices
PPTX
Angular js firebase-preso
PDF
Serverless Meetup - Event Sourcing
PPTX
Build software like a bag of marbles, not a castle of LEGO®
PDF
Crafting ColdFusion Applications like an Architect
PDF
Serverless in production, an experience report (microservices london)
PDF
Serverless in production, an experience report (London js community)
PPTX
Deploying microservices on AWS
PDF
Using BladeRunnerJS to Build Front-End Apps that Scale - Fluent 2014
PPTX
Building a [micro]services platform on AWS
PDF
Microservice Architecture
PDF
Serverless in production, an experience report (codemotion milan)
Fed London - January 2015
Patterns and practices for building enterprise-scale HTML5 apps
Reactive Micro Services with Java seminar
How to Build Front-End Web Apps that Scale - FutureJS
AngularJSTO presentation
Building front-end apps that Scale - FOSDEM 2014
AWS Lambda from the trenches (Serverless London)
Serverless in production, an experience report (BuildStuff)
Microservices: The Best Practices
Angular js firebase-preso
Serverless Meetup - Event Sourcing
Build software like a bag of marbles, not a castle of LEGO®
Crafting ColdFusion Applications like an Architect
Serverless in production, an experience report (microservices london)
Serverless in production, an experience report (London js community)
Deploying microservices on AWS
Using BladeRunnerJS to Build Front-End Apps that Scale - Fluent 2014
Building a [micro]services platform on AWS
Microservice Architecture
Serverless in production, an experience report (codemotion milan)

More from CocoaHeads Tricity (8)

PDF
[CocoaHeads Tricity] Michał Tuszyński - Modern iOS Apps
PDF
[CocoaHeads Tricity] Michał Zygar - Consuming API
PDF
[CocoaHeads Tricity] Maciej Burda - Working as an iOS developer Interview Cas...
PDF
2013-05-15 threads. why and how
PDF
2013-03-07 indie developer toolkit
PDF
2013-02-05 UX design for mobile apps
PDF
2013-01-10 iOS testing
PDF
2013-04-16 iOS development speed up
[CocoaHeads Tricity] Michał Tuszyński - Modern iOS Apps
[CocoaHeads Tricity] Michał Zygar - Consuming API
[CocoaHeads Tricity] Maciej Burda - Working as an iOS developer Interview Cas...
2013-05-15 threads. why and how
2013-03-07 indie developer toolkit
2013-02-05 UX design for mobile apps
2013-01-10 iOS testing
2013-04-16 iOS development speed up

Recently uploaded (20)

PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
AI in Product Development-omnex systems
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Introduction to Artificial Intelligence
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
medical staffing services at VALiNTRY
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Nekopoi APK 2025 free lastest update
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
top salesforce developer skills in 2025.pdf
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
ai tools demonstartion for schools and inter college
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
L1 - Introduction to python Backend.pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
AI in Product Development-omnex systems
Understanding Forklifts - TECH EHS Solution
Introduction to Artificial Intelligence
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
CHAPTER 2 - PM Management and IT Context
ISO 45001 Occupational Health and Safety Management System
medical staffing services at VALiNTRY
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Operating system designcfffgfgggggggvggggggggg
Nekopoi APK 2025 free lastest update
Adobe Illustrator 28.6 Crack My Vision of Vector Design
top salesforce developer skills in 2025.pdf
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
ai tools demonstartion for schools and inter college
VVF-Customer-Presentation2025-Ver1.9.pptx
How to Choose the Right IT Partner for Your Business in Malaysia
L1 - Introduction to python Backend.pptx

[CocoaHeads Tricity] Andrzej Dąbrowski - Dependency injection and testable architecture in Swift apps