100% found this document useful (1 vote)
142 views

iOS Test Driven Development by Tutorials First Edition Learn Real World Test Driven Development Joshua Greene 2024 scribd download

iOS

Uploaded by

ggggghodin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
142 views

iOS Test Driven Development by Tutorials First Edition Learn Real World Test Driven Development Joshua Greene 2024 scribd download

iOS

Uploaded by

ggggghodin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Experience Seamless Full Ebook Downloads for Every Genre at textbookfull.

com

iOS Test Driven Development by Tutorials First


Edition Learn Real World Test Driven Development
Joshua Greene

https://textbookfull.com/product/ios-test-driven-
development-by-tutorials-first-edition-learn-real-world-
test-driven-development-joshua-greene/

OR CLICK BUTTON

DOWNLOAD NOW

Explore and download more ebook at https://textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Test Driven Java Development 1st Edition Farcic Viktor


Garcia Alex

https://textbookfull.com/product/test-driven-java-development-1st-
edition-farcic-viktor-garcia-alex/

textboxfull.com

Test Driven JavaScript Development 1st Edition Gupta Ravi


Kumar Prajapati Hetal Singh Harmeet

https://textbookfull.com/product/test-driven-javascript-
development-1st-edition-gupta-ravi-kumar-prajapati-hetal-singh-
harmeet/
textboxfull.com

Test Driven Java Development Invoke TDD principles for end


to end application development 2nd Edition Alex Garcia

https://textbookfull.com/product/test-driven-java-development-invoke-
tdd-principles-for-end-to-end-application-development-2nd-edition-
alex-garcia/
textboxfull.com

Test-Driven Development in Go: A practical guide to


writing idiomatic and efficient Go tests through real-
world example 1st Edition Adelina Simion
https://textbookfull.com/product/test-driven-development-in-go-a-
practical-guide-to-writing-idiomatic-and-efficient-go-tests-through-
real-world-example-1st-edition-adelina-simion/
textboxfull.com
Thoughtful machine learning with Python a test driven
approach First Edition Kirk

https://textbookfull.com/product/thoughtful-machine-learning-with-
python-a-test-driven-approach-first-edition-kirk/

textboxfull.com

Python Testing Cookbook Easy solutions to test your Python


projects using test driven development and Selenium 2nd
Edition Greg L. Turnquist & Bhaskar N. Das
https://textbookfull.com/product/python-testing-cookbook-easy-
solutions-to-test-your-python-projects-using-test-driven-development-
and-selenium-2nd-edition-greg-l-turnquist-bhaskar-n-das/
textboxfull.com

Test Driven Development in Ruby A Practical Introduction


to TDD Using Problem and Solution Domain Analysis Paranj

https://textbookfull.com/product/test-driven-development-in-ruby-a-
practical-introduction-to-tdd-using-problem-and-solution-domain-
analysis-paranj/
textboxfull.com

Design Patterns by Tutorials Learning design patterns in


Swift 4 2 Joshua Greene

https://textbookfull.com/product/design-patterns-by-tutorials-
learning-design-patterns-in-swift-4-2-joshua-greene/

textboxfull.com

Test Driven Development with Python Obey the Testing Goat


Using Django Selenium and JavaScript 2nd Edition Harry J.
W. Percival
https://textbookfull.com/product/test-driven-development-with-python-
obey-the-testing-goat-using-django-selenium-and-javascript-2nd-
edition-harry-j-w-percival/
textboxfull.com
iOS Test-Driven Development by Tutorials

iOS Test-Driven Development by Tutorials


By Joshua Greene & Michael Katz

Copyright ©2019 Razeware LLC.

No7ce of Rights
All rights reserved. No part of this book or corresponding materials (such as text,
images, or source code) may be reproduced or distributed by any means without
prior written permission of the copyright owner.

No7ce of Liability
This book and all corresponding materials (such as source code) are provided on an
“as is” basis, without warranty of any kind, express of implied, including but not
limited to the warranties of merchantability, fitness for a particular purpose, and
noninfringement. In no event shall the authors or copyright holders be liable for any
claim, damages or other liability, whether in action of contract, tort or otherwise,
arising from, out of or in connection with the software or the use of other dealing in
the software.

Trademarks
All trademarks and registered trademarks appearing in this book are the property of
their own respective owners.

raywenderlich.com 2
iOS Test-Driven Development by Tutorials

Dedica7ons
"For my girls. I love you very much."

— Joshua Greene

"Dedicated to the memory of my mother-in-law, Barbara


Schwartz. Her selflessness and dedication to teaching inspires
me to give back to the community and educate others."

— Michael Katz

raywenderlich.com 3
iOS Test-Driven Development by Tutorials

About the Authors


Joshua Greene is an author of this book. He's an experienced
software developer and has created many mobile apps. When he's
not slinging code, you can find him wandering the streets of Tokyo.
You can reach him on Twitter @jrg_developer.

Michael Katz is a champion baker. ;] Oh, he's also an author of this


book, developer, architect, speaker, writer and avid homebrewer.
He has contributed to several books on iOS development and is a
long-time member of the raywenderlich.com tutorial team. He's
currently serving as director of mobile engineering at Viacom. He
shares his home state of New York with his family, the world's best
bagels and the Yankees. When he's not at his computer, he's out on
the trails, in his shop or reading a good book (like this one!).

About the Editors


Darren Ferguson is the final pass editor for this book. He is an
experienced software developer and works for M.C. Dean, Inc, a
systems integration provider from North Virginia. When he's not
coding, you'll find him enjoying EPL Football, traveling as much as
possible and spending time with his wife and daughter.

Manda Frederick is the editor of this book. She has been involved
in publishing for over ten years through various creative,
educational, medical and technical print and digital publications,
and is thrilled to bring her experience to the raywenderlich.com
family as Managing Editor. In her free time, you can find her at the
climbing gym, backpacking in the backcountry, hanging with her
dog, working on poems, playing guitar and exploring breweries.

raywenderlich.com 4
iOS Test-Driven Development by Tutorials

Jeff Rames is a tech editor for this book. He’s an enterprise


software developer in San Antonio, Texas who's focused on iOS for
nearly a decade. He spends his free time with his wife and
daughters, except when he abandons them for trips to Cape
Canaveral to watch rocket launches. Say hi on Twitter @jefframes!

James Taylor is a tech editor for this book. He’s an iOS developer
living in San Antonio, Texas with both his wife and daughter. He
enjoys bicycle touring around the United States and spending way
too much time on YouTube. You can find him on Twitter
@jamestaylorios.

About the Ar7st


Vicki Wenderlich is the designer and artist of the cover of this
book. She is Ray’s wife and business partner. She is a digital artist
who creates illustrations, game art and a lot of other art or design
work for the tutorials and books on raywenderlich.com. When she’s
not making art, she loves hiking, a good glass of wine and
attempting to create the perfect cheese plate.

raywenderlich.com 5
iOS Test-Driven Development by Tutorials

Table of Contents: Overview


Introduc7on ..................................................................................... 14
What You Need .............................................................................. 17
Book License ................................................................................... 18
Book Source Code & Forums ...................................................... 20
Sec7on I: Hello, TDD! ..................................................... 22
Chapter 1: What Is TDD? .................................................. 23
Chapter 2: The TDD Cycle ................................................ 28
Sec7on II: Beginning TDD ............................................. 42
Chapter 3: TDD App Setup ............................................... 43
Chapter 4: Test Expressions .............................................. 63
Chapter 5: Test Expecta7ons ............................................ 89
Chapter 6: Dependency Injec7on & Mocks ................ 116
Sec7on III: TDD with Networking ............................ 140
Chapter 7: Introducing Dog Patch ................................ 141
Chapter 8: Networking client ......................................... 146
Chapter 9: Using the Network Client ........................... 174
Chapter 10: Image Client ................................................ 193
Sec7on IV: TDD in Legacy Apps ................................ 228
Chapter 11: Legacy Problems ........................................ 230
Chapter 12: Dependency Maps ..................................... 254
Chapter 13: Breaking Up Dependencies ..................... 270

raywenderlich.com 6
iOS Test-Driven Development by Tutorials

Chapter 14: Modularizing Dependencies .................... 290


Chapter 15: Adding Features to Exis7ng Classes ...... 307

raywenderlich.com 7
iOS Test-Driven Development by Tutorials

Table of Contents: Extended


Introduc7on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
About this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Sec7on introduc7ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
How to read this book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
What You Need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Book License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Book Source Code & Forums . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Sec7on I: Hello, TDD! . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Chapter 1: What Is TDD? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Why should you use TDD? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
What should you test? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
But TDD takes too long! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
When should you use TDD? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Chapter 2: The TDD Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Ge_ng started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Red: Write a failing test. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Green: Make the test pass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Refactor: Clean up your code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Repeat: Do it again . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
TDDing init(availableFunds:) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
TDDing addItem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Adding two items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Challenge. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

Sec7on II: Beginning TDD . . . . . . . . . . . . . . . . . . . . . . 42

raywenderlich.com 8
iOS Test-Driven Development by Tutorials

Chapter 3: TDD App Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43


About the FitNess app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Your first test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Red-Green-Refactor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Test nomenclature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Structure of XCTestCase subclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Your next set of tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Using @testable import. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Tes7ng ini7al condi7ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Refactoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Challenge. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Chapter 4: Test Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Assert methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
View controller tes7ng . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Test ordering mahers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Code coverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Debugging tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Challenge. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Chapter 5: Test Expecta7ons. . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Using an expecta7on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Tes7ng for true asynchronicity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Wai7ng for no7fica7ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Showing the alert to a user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Ge_ng specific about no7fica7ons . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Driving alerts from the data model . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Using other types of expecta7ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

raywenderlich.com 9
iOS Test-Driven Development by Tutorials

Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115


Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Chapter 6: Dependency Injec7on & Mocks . . . . . . . . . . . . . 116
What's up with fakes, mocks, and stubs? . . . . . . . . . . . . . . . . . . . . . . 116
Understanding CMPedometer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Mocking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Handling error condi7ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Ge_ng actual data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Making a func7onal fake . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Wiring up the chase view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Time dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

Sec7on III: TDD with Networking . . . . . . . . . . . . . . 140


Chapter 7: Introducing Dog Patch . . . . . . . . . . . . . . . . . . . . . 141
Ge_ng started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Understanding Dog Patch's architecture . . . . . . . . . . . . . . . . . . . . . . . 144
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Chapter 8: Networking client . . . . . . . . . . . . . . . . . . . . . . . . . 146
Ge_ng Started. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Se_ng up the networking client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
TDDing the networking call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Dispatching to a response queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Chapter 9: Using the Network Client . . . . . . . . . . . . . . . . . . 174
Ge_ng started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Crea7ng a shared instance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Adding a network client property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

raywenderlich.com 10
iOS Test-Driven Development by Tutorials

Using the network client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178


Crea7ng the network client protocol . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Crea7ng the mock network client . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Using the mock network client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Chapter 10: Image Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Ge_ng started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Se_ng up the image client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Crea7ng an image client protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Downloading an image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Se_ng an image view from a URL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Using the image client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227

Sec7on IV: TDD in Legacy Apps . . . . . . . . . . . . . . . . 228


Chapter 11: Legacy Problems . . . . . . . . . . . . . . . . . . . . . . . . . 230
Introducing MyBiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Iden7fying a change point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Finding a test point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Breaking dependencies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Wri7ng tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Making a change and refactoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Chapter 12: Dependency Maps . . . . . . . . . . . . . . . . . . . . . . . 254
Ge_ng started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Choosing where to begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Finding direct dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Finding secondary dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

raywenderlich.com 11
iOS Test-Driven Development by Tutorials

Deciding when to stop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263


What are problema7c dependencies? . . . . . . . . . . . . . . . . . . . . . . . . . 264
Finding problema7c dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Comple7ng the map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Breaking up complex systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Chapter 13: Breaking Up Dependencies . . . . . . . . . . . . . . . . 270
Ge_ng started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Characterizing the system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Breaking up the API/AppDelegate dependency . . . . . . . . . . . . . . . . . 274
Breaking the AppDelegate dependency . . . . . . . . . . . . . . . . . . . . . . . 279
Breaking the ErrorViewController dependency . . . . . . . . . . . . . . . . . 282
Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Key Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Chapter 14: Modularizing Dependencies . . . . . . . . . . . . . . . 290
Moving files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Using the new framework with Login . . . . . . . . . . . . . . . . . . . . . . . . . 296
Fixing MyBiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Wrap up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Chapter 15: Adding Features to Exis7ng Classes . . . . . . . . 307
Ge_ng started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Sending reports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Adding analy7cs to the view controllers . . . . . . . . . . . . . . . . . . . . . . . 315
Passing around dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323

raywenderlich.com 12
iOS Test-Driven Development by Tutorials

Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323


Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323

raywenderlich.com 13
I Introduc7on

Welcome to iOS Test-Driven Development by Tutorials! This book will teach you all
about test-driven development (TDD) — the art of turning requirements into tests
and tests into production code.

You'll get hands-on TDD experience by creating three real-world apps in this book:

By the end of this book, you'll have a strong understanding of TDD and be able to
apply this knowledge to your own apps.

raywenderlich.com 14
iOS Test-Driven Development by Tutorials Introduction

About this book


We wrote this book with beginner-to-intermediate developers in mind. The only
requirements for reading this book are a basic understanding of Swift and iOS
development.

If you’ve worked through our classic beginner books — the Swift Apprentice https://
store.raywenderlich.com/products/swift-apprentice and the iOS Apprentice https://
store.raywenderlich.com/products/ios-apprentice — or have similar development
experience, you’re ready to read this book. You'll also benefit from a working
knowledge of design patterns — such as working through Design Patterns by Tutorials
https://store.raywenderlich.com/products/design-patterns-by-tutorials — but this
isn't strictly required.

As you work through this book, you’ll progress from beginner topics to more
advanced concepts.

Sec7on introduc7ons
I. Introduc7on
This is a high-level introduction to TDD, explaining why it's important and how it
will help you.

You'll also be introduced to the TDD Cycle in this section. This is the foundation for
how TDD works and guiding principles on the best way to apply it.

II. Beginning TDD


You'll learn the basics of TDD in this section, including XCTest, test expressions,
mocks and test expectations.

The chapters in this section build an example app called Fitness. This is the premier
fitness-coaching app based on the "Loch Ness" workout: You'll have to outrun,
outswim and outclimb Nessie (or get eaten)!

raywenderlich.com 15
iOS Test-Driven Development by Tutorials Introduction

III. TDD with Networking


You'll learn about TDD and networking in this section, including writing tests for
RESTful networking calls, downloading images and using networking clients.

You'll create an app called Dog Patch throughout this section. Dog Patch lets dog
lovers everywhere connect with kind breeders to help get the dog of their dreams.

IV. TDD in Legacy Apps


This section will teach you how to start TDD in a legacy app that wasn't created with
TDD and doesn't have sufficient test coverage.

You'll update an app called MyBiz throughout this section. MyBiz is an enterprise
resource planning (ERP) app for running a business, including employee
management and scheduling, time tracking, payroll and inventory management.

How to read this book


If you're new to unit testing or TDD, you should read this book from cover to cover.

If you already have some experience with TDD, you can skip from chapter to chapter
or use this book as a reference. You'll always be provided with a starter project in
each chapter to get up and running quickly.

What's the absolute best way to read this book? Just start reading wherever makes
sense to you!

raywenderlich.com 16
W What You Need

To follow along with this book, you'll need the following:

• Xcode 11 or later. Xcode is the main development tool for writing code in Swift.
You need Xcode 11 at a minimum, since that version includes Swift 5.1. You can
download the latest version of Xcode for free from the Mac App Store, here:
apple.co/1FLn51R.

If you haven't installed the latest version of Xcode, be sure to do that before
continuing with the book. The code covered in this book depends on Swift 5.1 and
Xcode 11 — the code may not compile if you try to work with an older version.

raywenderlich.com 17
L Book License

By purchasing iOS Test-Driven Development by Tutorials, you have the following


license:

• You are allowed to use and/or modify the source code in iOS Test-Driven
Development by Tutorials in as many apps as you want, with no attribution
required.

• You are allowed to use and/or modify all art, images and designs that are included
in iOS Test-Driven Development by Tutorials in as many apps as you want, but must
include this attribution line somewhere inside your app: “Artwork/images/designs:
from iOS Test-Driven Development by Tutorials, available at
www.raywenderlich.com”.

• The source code included in iOS Test-Driven Development by Tutorials is for your
personal use only. You are NOT allowed to distribute or sell the source code in iOS
Test-Driven Development by Tutorials without prior authorization.

• This book is for your personal use only. You are NOT allowed to sell this book
without prior authorization, or distribute it to friends, coworkers or students; they
would need to purchase their own copies.

All materials provided with this book are provided on an “as is” basis, without
warranty of any kind, express or implied, including but not limited to the warranties
of merchantability, fitness for a particular purpose and noninfringement. In no event
shall the authors or copyright holders be liable for any claim, damages or other
liability, whether in an action or contract, tort or otherwise, arising from, out of or in
connection with the software or the use or other dealings in the software.

raywenderlich.com 18
iOS Test-Driven Development by Tutorials Book License

All trademarks and registered trademarks appearing in this guide are the properties
of their respective owners.

raywenderlich.com 19
B Book Source Code &
Forums

If you bought the digital edi7on


The digital edition of this book comes with the source code for the starter and
completed projects for each chapter. These resources are included with the digital
edition you downloaded from https://store.raywenderlich.com/products/ios-test-
driven-development.

If you bought the print version


You can get the source code for the print edition of the book here:

• https://store.raywenderlich.com/products/ios-test-driven-development-source-
code

Forums
We’ve also set up an official forum for the book at forums.raywenderlich.com.This is
a great place to ask questions about the book or to submit any errors you may find.

Digital book edi7ons


We have a digital edition of this book available in both ePUB and PDF, which can be
handy if you want a soft copy to take with you, or you want to quickly search for a
specific term within the book.

raywenderlich.com 20
iOS Test-Driven Development by Tutorials Book Source Code & Forums

Buying the digital edition version of the book also has a few extra benefits: free
updates each time we update the book, access to older versions of the book, and you
can download the digital editions from anywhere, at anytime.

Visit our iOS Test-Driven Development store page here:

• https://store.raywenderlich.com/products/ios-test-driven-development.

And if you purchased the print version of this book, you’re eligible to upgrade to the
digital editions at a significant discount! Simply email [email protected] with
your receipt for the physical copy and we’ll get you set up with the discounted digital
edition version of the book.

raywenderlich.com 21
Sec7on I: Hello, TDD!

This section is a high-level introduction to test-driven development, how it works


and why you should use it. You'll also learn about the TDD cycle in this chapter, and
you'll use this throughout the rest of the book.

• Chapter 1: What Is TDD?: Test-driven development, or TDD, is an iterative way


to develop software by making many small changes backed by tests.

• Chapter 2: The TDD Cycle: TDD has four steps known as the Red-Green-
Refactor Cycle.

raywenderlich.com 22
1 Chapter 1: What Is TDD?
By Joshua Greene

Test-driven development, or TDD, is an iterative way to develop software by


iteratively making many small changes backed by tests.

It has four steps:

1. Write a failing test

2. Make the test pass

3. Refactor

4. Repeat

This is called the TDD Cycle. It ensures you thoroughly and accurately test your
code because your development is... driven by testing!

raywenderlich.com 23
iOS Test-Driven Development by Tutorials Chapter 1: What Is TDD?

By writing a test followed by the production code to make it pass, you ensure your
production code is testable and that it meets all of your requirements during
development. As an added bonus, your tests act as documentation for your
production code, describing how it works.

On the surface, the TDD process seems pretty simple. Well, I’m sorry to tell you
that... wait, it actually is really simple!

Sure, there are special circumstances for how to implement this cycle at times, but
that’s where this book comes in! Once you get the hang of this process, it will
become second nature. You’ll learn a lot more about this process in the next chapter.

Why should you use TDD?


TDD is the single best way to ensure your software works and continues to work well
into the future — well, that’s quite a bold claim! Let me explain.

It’s hard to argue against testing your code, but you don’t have to follow TDD to do
this. For example, you could write all of your production code and then write all of
your tests. Alternatively, you could skip writing tests altogether and, instead,
manually test your code. Why is TDD better than these options?

Good tests ensure your app works as expected. However, not all tests are "good."
Writing tests for the sake of having tests isn’t a worthwhile exercise. Rather, good
tests are failable, repeatable, quick to run and maintainable.

TDD provides methodology that ensures your tests are good:

• The first step is to write a failing test. By definition, this proves the test is failable.
Tests that can’t fail aren’t very useful. Rather, they waste valuable CPU time.

• Before you’re allowed to write a new test, all other previous tests must pass. This
ensures that your tests are repeatable: You don’t just run the single test you’re
working on, but rather, you constantly run all of the tests.

• By frequently running every test, you’re incentivized to make sure tests are quick
to run. All of your tests should take seconds to run — preferably, one second or
less.

A single test that takes a hundred milliseconds is too slow: After only ten tests,
your entire test suite will take one second to run. After fifty tests, it takes five
seconds. After several seconds, no one runs all of the tests because it takes too
long.

raywenderlich.com 24
iOS Test-Driven Development by Tutorials Chapter 1: What Is TDD?

• When you refactor, you update both your production and test code. This ensures
your tests are maintained: You’re constantly keeping them up-to-date.

• By iteratively writing production code and tests in parallel, you ensure your code is
testable. If you were to write tests after completing the code, it’s likely the
production code would require quite a bit of refactoring to fully unit test.

Nonetheless, the devil’s advocate in you may say, "But you could write good tests
without following TDD." You definitely could, but you may struggle to succeed. You
can definitely do it in the short term, but it’s much more difficult in the long term.
You’d need to be disciplined about writing good tests. Before long, you’d likely create
some sort of system to ensure that you’re writing good tests... you’d likely find
yourself doing a variant of TDD!

What should you test?


Better test coverage doesn’t always mean your app is better tested. There are things
you should test and others you shouldn’t. Here are the do’s and don’ts:

• Do write tests for code that can’t be caught in an automated fashion otherwise.
This includes code in your classes’ methods, custom getters and setters and most
anything else you write yourself.

• Don’t write tests for generated code. For example, it’s not worthwhile to write
tests for generated getters and setters. Swift does this very well, and you can trust
it works.

• Don’t write tests for issues that can be caught by the compiler. If the tested issue
would generate an error or warning, Xcode will catch it for you.

• Don’t write tests for dependency code, such as first- or third-party frameworks
your app uses. The framework authors are responsible for writing those tests. For
example, you shouldn’t write tests for UIKit classes because UIKit developers are
responsible for writing these. However, you should write tests for your custom
subclasses thereof: This is your custom code, so you’re responsible for writing the
tests.

An exception to the above is writing tests in order to determine how a framework


works. This can be very useful to do. However, you don’t need to keep these tests
long term. Rather, you should delete them afterwards.

Another exception is "sanity tests" that prove third-party code works as you expect.
These sort of tests are useful if the library isn’t fully stable, or you don’t trust it

raywenderlich.com 25
iOS Test-Driven Development by Tutorials Chapter 1: What Is TDD?

entirely. In either case, you should really scrutinize whether or not you want to use
the library at all — is there a better option that’s more trustworthy?

But TDD takes too long!


The most common complaint about TDD is that it takes too long — usually followed
by exclamation point(s) or sad-face emojis.

Fortunately, TDD gets faster once you get used to doing it. However, the truth is that
compared to not writing any tests at all, you’re writing more code ultimately. It likely
will take a little more time to develop initially.

That said, there’s a really big hole in this argument: The real time cost of
development isn’t just writing the initial, first-version production code. It also
includes adding new features over time, modifying existing code, fixing bugs and
more. In the long run, following TDD takes much less time than not following it
because it yields more maintainable code with fewer bugs.

There’s also another cost to consider: customer impact of bugs in production. The
longer an issue goes undiscovered, the more expensive it is. It can result in negative
reviews, lost trust and lost revenue.

If an issue is caught during development, it’s easier to debug and quicker to fix. If
you discovered it weeks later, you’d spend substantially more time getting up to
speed on the code and tracking down the root cause. By following TDD, your tests
ultimately help safeguard and protect your app against bugs.

When should you use TDD?


TDD can be used during any point in a product’s life cycle: new development, legacy
apps and everything in between. However, how and where you start TDD does
depend on the state of your project. This book will cover how to approach many of
these situations!

However, an important question to ask: Should your project use TDD at all?

As a general rule of thumb, if your app is going to last more than a few months, will
have multiple releases and/or require complex logic, you’re likely better off using
TDD than not.

raywenderlich.com 26
iOS Test-Driven Development by Tutorials Chapter 1: What Is TDD?

If you’re creating an app for a hackathon, test project or something else that’s meant
to be temporary, you should evaluate whether TDD makes sense. If there’s really only
going to be one version of the app, you might not follow TDD or might only do TDD
for critical or difficult parts.

Ultimately, TDD is a tool, and it’s up to you to decide when it’s best to use it!

Key points
In this chapter, you learned what TDD is, why you should use it, what to test and
when to use it. Here are the key points to remember:

• TDD offers a consistent method to write good tests.

• Goods tests are failable, repeatable, quick to run and maintainable.

• Write tests for code that you’re responsible for maintaining. Don’t test code that’s
automatically generated or code within dependencies.

• The real cost of development includes initial coding time, adding new features
over time, modifying existing code, fixing bugs and more. TDD reduces
maintenance costs and quantity of bugs, often making it the most cost effective
approach.

• TDD is most useful for long-term projects lasting more than a few months or
having multiple releases.

raywenderlich.com 27
2 Chapter 2: The TDD Cycle
By Joshua Greene

In the previous chapter, you learned that test-driven development boils down to a
simple process called the TDD Cycle. It has four steps that are often "color coded" as
follows:

1. Red: Write a failing test, before writing any app code.

2. Green: Write the bare minimum code to make the test pass.

3. Refactor: Clean up both your app and test code.

4. Repeat: Do this cycle again until all features are implemented.

This is also called the Red-Green-Refactor Cycle.

raywenderlich.com 28
iOS Test-Driven Development by Tutorials Chapter 2: The TDD Cycle

Why is it color coded? This corresponds to the colors shown in most code editors,
including Xcode:

• Failing tests are indicated with a red X.

• Passing tests are shown with a green checkmark.

This chapter provides an introduction to the TDD Cycle, which you'll use throughout
the rest of this book. However, it doesn't go into detail about test expressions
(XCTAssert, et al.) or how to set up a test target. Rather, these topics are covered in
later chapters. For now, focus on learning the TDD Cycle, and you'll learn the rest as
you go along.

It's best to learn by doing, so let's jump straight into code!

GeXng started
In this chapter, you'll create a simple version of a cash register to learn the TDD
Cycle. To keep the focus on TDD instead of Xcode setup, you'll use a playground.
Open CashRegister.playground in the starter directory, then open the
CashRegister page. You'll see this page has two imports, but otherwise it's empty.

Naturally, you'll begin with the first step in the TDD Cycle: red.

Red: Write a failing test


Before you write any production code, you must first write a failing test. To do so,
you need to create a test class. Add the following below the import statements:

class CashRegisterTests: XCTestCase {

Above, you declare CashRegisterTests as a subclass of XCTestCase, which is part


of the XCTest framework. You'll almost always subclass XCTestCase to create your
test classes.

raywenderlich.com 29
Another Random Scribd Document
with Unrelated Content
määrin. Kaupungin ahtaiden katujen varsilla, missä useimmat talot
ovat vielä vanhaan tapaan rakennetut, näkyy lukuisten venäläisten
kauppiasten ja käsityöläisten nimikilpien joukossa myös sellaisia
kirjoituksia kuin: Bottier de Paris (pariisilainen suutari) ja Marchande
de Modes (muotikauppiatar). Sellainen rehentely ei kuitenkaan voine
pettää herkkäuskoistakaan. Tavara, mitä näistä kaupoista voi ostaa,
on sellaista, ettei piintyneinkään takalistolainen voi olla epätietoinen
sen alkuperästä.

Perille päästyä johti liikkeelläolo Assanoffin ajatukset viimein


vähän toisaalle. Hän ravisti itsensä virkeäksi ja osoitti jälleen entistä
luonnonlaatuaan, toisin sanoen: hän alkoi jälleen elää. Moreno
esiteltiin everstillensä, toverit ottivat hänet hyvin vastaan ja
eurooppalaiset juhlivat häntä, ja ollessaan välttämättömyyden pakon
alaisena hän tuli vähemmän kuin siihen asti katsoneeksi
menneisyyteen. Kolmen kuukauden kuluttua hänellä oli jälleen
luutnantin olkalaput. Hän otti osaa erääseen retkeen, suoritti siinä
hyvin tehtävänsä ja yleni ratsumestariksi. Sotilaalla on oma tapansa
katsella elämää: jos hänelle lupaisi paratiisin korvaukseksi
virkavuosioikeuksista ja helvetin jostakin korkeammasta asemasta,
niin tuskin sittenkään kukaan epäröisi, ja jos joku pitäisikin
parempana olla Jumalan luona, ei hän ikinä lakkaisi katumasta
uhraustaan.

Sittenkin Don Juan antoi vielä vuosikausia sydämensä


toivomusten harhailla Espanjaan. Tosin ei hänen rakkautensa
aiheuttanut hänelle enää samaa kalvavaa tuskaa kuin ensimmäisinä
kuukausina. Se muuttui hempeäksi tottumukseksi, melankoliseksi
tunnelmaksi, josta hänen sielunsa sai tavallaan tyydytystä. Hän
kirjoitti usein ja sai myös vastauksia. Rakastavat toivoivat parhaansa
mukaan erillään olonsa loppumista. Kun politiikka hautasi sen
tapparan, joka oli katkaissut heidän sydämiensä siteen, täytyi heidän
pian huomata, etteivät aineelliset elinehdot sallineet Morenon jättää
Kaukaasiaa, kun hänellä ei ollut muuta kuin palkkansa eikä hän
enää voinut muuttaa elämänuraa; nuori nainen ei myöskään ollut
kyllin rikas voidakseen tulla lemmittynsä luo. Niin jäi kaikki
entiselleen. Ei kumpikaan mennyt naimisiin. Vähitellen he lakkasivat
tuntemasta itseään perin onnettomiksi, mutta onnellisia ei heistä
tullut koskaan.

*****

Kauan ennen sitä aikaa, josta nyt on puhe, tuli Moreno eräänä
yönä jokseenkin myöhään kotiin iltakutsuista kenraalikuvernöörin
luota ja näki kaukaa autiolla kadulla, joka kulkee tataarilaiskaanin
entisen, nyt ruutimakasiiniksi muutetun palatsin ohi, naisolennon,
joka astui samaan suuntaan kuin hänkin. Oli talvi ja kylmä, lumi peitti
maata useampien tuumain vahvuudelta, kaikki oli jäässä ja yö
synkimmillään.

Moreno arveli itsekseen: — Kukahan tuo onneton olento lienee?

Ratsumestari oli nähnyt paljon kurjuutta ja onnettomuutta


lähimmässä ympäristössään, eikä hänen omassakaan elämässään
ollut iloa. Sellaisissa olosuhteissa muuttuu ihminen joko huonoksi tai
erikoisen kunnolliseksi. Morenosta oli tullut kunnon mies.

Niin paljon kuin pimeys salli, tarkkasivat hänen silmänsä säälien


tuon olennon yksinäistä kulkua. Huomatessaan, että nainen pysähtyi
ja horjahteli, hän kiirehti askeleitaan saavuttaakseen hänet ja
auttaakseen, mutta samassa hän suureksi hämmästyksekseen
havaitsi, että onneton olento pysähtyi juuri hänen ovensa eteen, ja
kohta kuului myös hänen takaansa nopeita askeleita.
Moreno kääntyi ja tunsi heti 'hengen vihollisen'. Gregor Ivanitsh oli
paljain päin, turkitta ja liikkui niin nopeasti eteenpäin kuin hänen
suuresti lisääntynyt ruumiinpainonsa salli. Moreno luuli, eikä ilman
syytä, että "hengen vihollinen" oli tuota naista tavoittamassa, ja
hänen päähänsä pälkähti, että se saattaisi tapahtua huonossa
tarkoituksessa.

Hän tarttui siis Gregor Ivanitshin käteen ja huusi kovaa: "Minne


matka?"

"Herra ratsumestari, pyydän teiltä, älkää pidättäkö minua. Tyttö


parka on karkuteillä."

"Kuka? Mistä tytöstä te puhutte?"

"Selityksiin ei nyt ole aikaa, herra ratsumestari, mutta kun kerran


olette tässä, niin auttakaa minua pelastamaan hänet. Ehkä se on
vielä mahdollista, voi, ja jos kukaan pystyy häntä rauhoittamaan, niin
juuri te!"

Hän veti Morenoa mukanaan. Tämä ei pannut vastaan,


hämmästynyt kun oli, ja ollessaan vain muutaman askeleen päässä
kodistaan hän huomasi kauhukseen, kuinka nainen ojensi kätensä
ovea vasten pysyäkseen pystyssä ja samalla horjahteli: seuraavassa
silmänräpäyksessä hän varmasti syöksyisi kynnyksen yli. Moreno
tarttui häneen, kietoi käsivartensa hänen ympärilleen ja katsoi häntä
kasvoihin. Nainen oli Umm Djehan.

Tuntiessaan Morenon hän sai kuin sähköiskun, joka lyhyeksi


hetkeksi salaman tavoin antoi hänelle uutta voimaa. Umm Djehan
kietoi käsivartensa Morenon kaulaan, suuteli häntä tulisesti eikä
virkkanut muuta kuin ainoan sanan: "Hyvästi!"
Sitten hänen käsivartensa heltisivät, ja hän vaipui taaksepäin.
Moreno katsoi häneen tyrmistyneenä ja huomasi, että hän oli kuollut.

Samassa tuli Gregor Ivanitsh paikalle ja auttoi häntä pitämään


elotonta ruumista pystyssä. Moreno tahtoi viedä hänet asuntoonsa.

"Ei", sanoi 'hengen vihollinen' päätään pudistaen, "lapsi parka on


minun luonani ollut sairaana, sentähden tahdon myös hautauttaa
hänet omalla kustannuksellani. Nyt hän on kuollut. Hän ei ole minua
rakastanut, mutta minä tahdoin hänen parastaan, ja siinä on syytä
kyllin, että saan pitää itseäni hänen ainoana omaisenaan."

"Mitä sitten oikeastaan on tapahtunut?" kysyi Moreno.

"Eipä paljoa. Hän ei halunnut joutua myydyksi, hän ei suostunut


lähtemään Trapezuntiin. Hän kieltäytyi tanssimasta, ja mitä ei
koskaan ennen ollut tapahtunut, mitä ei koskaan ennen oltu hänessä
nähty, hän kulutti yöt ja päivät itkien, hän raastoi rintojaan ja repi
kynsillä kasvojaan. Kauneudenloiste ei tiennyt enää kuinka tulisi
hänen kanssaan toimeen ja halusi kovasti päästä hänestä eroon.
Silloin sanoin puolestani Umm Djehanille: 'Tyttäreni, olet väärällä
tiellä, ja juuri henki saa ilmeisesti pääsi pyörälle. Unohda tyhmät
ajatukset! Juo, naura, laula, huvittele, anna oikuillesi täysi valta! Olet
nuori, olet kaunis, herätät ihailua ja tanssit kuin haltiatar. Kenraalikin
makaa jalkojesi juuressa, jos haluat. Minkätähden oikeastaan et
tahdo?"

Hän vastasi minulle: 'Koska rakastan saamatta vastarakkautta.'

Enempää selvää ei hänestä voinut saada. Minä, joka olin ennen


ollut häneen rakastunut, en enää siitä välittänyt, vaan otin hänet
kaikessa ystävyydessä huostaani ja vein vuokratilalleni hänen
omalla suostumuksellaan. Hoidin häntä, koetin viihdytellä — mutta
mitä arvelette — pelkästä itkemisestä hän viimein alkoi yskiä ja minä
haetin lääkärin, joka selitti, että hänen oli oltava hyvin varovainen ja
vältettävä kylmettymistä. Tiedättekö, mitä hän teki? Hän meni ulos ja
piehtaroi lumessa! Niin, henki, henki! Älkää minulle enää siitä
puhuko! Tehän olette kaikki sokeita, pakanoita! Viimein, kolme
päivää sitten, hän sanoi minulle toden totta sen, mitä teille nyt
kerron. Se on sulaa järjettömyyttä, mutta kuitenkin toistan tarkoin
hänen omat sanansa. Hän sanoi minulle: 'Vie minut Bakuun!'

'Mitäs sinä sieltä haluat?' kysyin.

'Kuolla', vastasi hän.

Tuska ahdisti hänen kurkkuaan ja minä vastasin tylysti: 'Täällä on


yhtä hyvä kuolla kuin Bakussa.'

'Ei ole! Tahdon kuolla ratsumestari Morenon kynnykselle.'

Luulin hänen puhuvan sekapäisenä. Koskaan hän ei ollut lausunut


nimeänne, ei koskaan, sanon teille, ei kertaakaan. Hän suuttui ja
vastasi minulle vihaisesti: 'Etkö ymmärrä minua?'

Kun hän suuttui, tuli häneltä verta suusta, ja hänellä oli tuntikausia
tuskia. Annoin siis perään.

'No, hyvä, mennään vain!'

"Tulimme tänne. Hän lähetti minut juuri äsken apua hakemaan


sanoen voivansa huonommin, mikä ikävä kyllä oli liiankin totta. Ja
totellessani häntä… niin, näettehän itse."

Huokaus katkaisi miesparalta äänen.


Moreno tunsi syvää surua. Se ei tosin ollut järkevää. Suurin onni,
mikä voi Umm Djehania kohdata, oli tullut hänen osakseen. Mitä
olisikaan voinut hänestä tulla elämässä? Jos hän olisi pysynyt
todellisena ja uskollisena lesgittärenä, ei Assanoffin ja
nuoruudenunelmain menettäminen olisi murtanut hänen sydäntään.
Hän oli paljon kärsinyt, hän olisi vastedeskin epäilemättä saanut
kärsiä, mutta hänen tyydytetty ylpeytensä ja varmentunut
itsetietoisuutensa olisivat pitäneet häntä pystyssä loppuun saakka, ja
joko hän sitten olisi tanssitaitonsa tenholla lumonnut shemahalaisia
asiantuntijoita tai antanut etusijan vanhan kaimakamin maailmaa
vierovalle haaremille, niin hän olisi joka tapauksessa päässyt
korkeaan ikään ja voinut elämäniltana vaipua kuten vanhan
patriarkan vaimot rauhalliseen ja kunniakkaaseen kuolemanuneen.
Mutta hänkin oli lopulta tullut uskottomaksi kotimaansa jumalille. Hän
oli kyllä ponnistellut vastaan, kapinoinut ja urhoollisesti kaatunut
tämän vastarintansa uhrina; mutta siitä huolimatta pitää kuitenkin
paikkansa, että hän oli sydämensä pohjassa käynyt heikoksi: hän oli
rakastunut eurooppalaiseen!

Kun Moreno kertoi Assanoffille koko tarinan, koski se


sivistyneeseen tataariin tavattoman voimakkaasti. Viikkoon hän ei
selvinnyt, ja missä hänet tapasikin, hän lauloi marseljeesia.
Myöhemmin hän rauhoittui.
*** END OF THE PROJECT GUTENBERG EBOOK
KAUKAASIALAINEN TANSSIJATAR ***

Updated editions will replace the previous one—the old editions will
be renamed.

Creating the works from print editions not protected by U.S.


copyright law means that no one owns a United States copyright in
these works, so the Foundation (and you!) can copy and distribute it
in the United States without permission and without paying copyright
royalties. Special rules, set forth in the General Terms of Use part of
this license, apply to copying and distributing Project Gutenberg™
electronic works to protect the PROJECT GUTENBERG™ concept
and trademark. Project Gutenberg is a registered trademark, and
may not be used if you charge for an eBook, except by following the
terms of the trademark license, including paying royalties for use of
the Project Gutenberg trademark. If you do not charge anything for
copies of this eBook, complying with the trademark license is very
easy. You may use this eBook for nearly any purpose such as
creation of derivative works, reports, performances and research.
Project Gutenberg eBooks may be modified and printed and given
away—you may do practically ANYTHING in the United States with
eBooks not protected by U.S. copyright law. Redistribution is subject
to the trademark license, especially commercial redistribution.

START: FULL LICENSE


THE FULL PROJECT GUTENBERG LICENSE
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the free


distribution of electronic works, by using or distributing this work (or
any other work associated in any way with the phrase “Project
Gutenberg”), you agree to comply with all the terms of the Full
Project Gutenberg™ License available with this file or online at
www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand, agree
to and accept all the terms of this license and intellectual property
(trademark/copyright) agreement. If you do not agree to abide by all
the terms of this agreement, you must cease using and return or
destroy all copies of Project Gutenberg™ electronic works in your
possession. If you paid a fee for obtaining a copy of or access to a
Project Gutenberg™ electronic work and you do not agree to be
bound by the terms of this agreement, you may obtain a refund from
the person or entity to whom you paid the fee as set forth in
paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only be


used on or associated in any way with an electronic work by people
who agree to be bound by the terms of this agreement. There are a
few things that you can do with most Project Gutenberg™ electronic
works even without complying with the full terms of this agreement.
See paragraph 1.C below. There are a lot of things you can do with
Project Gutenberg™ electronic works if you follow the terms of this
agreement and help preserve free future access to Project
Gutenberg™ electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright law in
the United States and you are located in the United States, we do
not claim a right to prevent you from copying, distributing,
performing, displaying or creating derivative works based on the
work as long as all references to Project Gutenberg are removed. Of
course, we hope that you will support the Project Gutenberg™
mission of promoting free access to electronic works by freely
sharing Project Gutenberg™ works in compliance with the terms of
this agreement for keeping the Project Gutenberg™ name
associated with the work. You can easily comply with the terms of
this agreement by keeping this work in the same format with its
attached full Project Gutenberg™ License when you share it without
charge with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the terms
of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.

1.E. Unless you have removed all references to Project Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project Gutenberg™
work (any work on which the phrase “Project Gutenberg” appears, or
with which the phrase “Project Gutenberg” is associated) is
accessed, displayed, performed, viewed, copied or distributed:
This eBook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this eBook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is derived


from texts not protected by U.S. copyright law (does not contain a
notice indicating that it is posted with permission of the copyright
holder), the work can be copied and distributed to anyone in the
United States without paying any fees or charges. If you are
redistributing or providing access to a work with the phrase “Project
Gutenberg” associated with or appearing on the work, you must
comply either with the requirements of paragraphs 1.E.1 through
1.E.7 or obtain permission for the use of the work and the Project
Gutenberg™ trademark as set forth in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is posted


with the permission of the copyright holder, your use and distribution
must comply with both paragraphs 1.E.1 through 1.E.7 and any
additional terms imposed by the copyright holder. Additional terms
will be linked to the Project Gutenberg™ License for all works posted
with the permission of the copyright holder found at the beginning of
this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files containing a
part of this work or any other work associated with Project
Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute this


electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1 with
active links or immediate access to the full terms of the Project
Gutenberg™ License.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or expense
to the user, provide a copy, a means of exporting a copy, or a means
of obtaining a copy upon request, of the work in its original “Plain
Vanilla ASCII” or other form. Any alternate format must include the
full Project Gutenberg™ License as specified in paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™ works
unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or providing


access to or distributing Project Gutenberg™ electronic works
provided that:

• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt that
s/he does not agree to the terms of the full Project Gutenberg™
License. You must require such a user to return or destroy all
copies of the works possessed in a physical medium and
discontinue all use of and all access to other copies of Project
Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project Gutenberg™


electronic work or group of works on different terms than are set
forth in this agreement, you must obtain permission in writing from
the Project Gutenberg Literary Archive Foundation, the manager of
the Project Gutenberg™ trademark. Contact the Foundation as set
forth in Section 3 below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on, transcribe
and proofread works not protected by U.S. copyright law in creating
the Project Gutenberg™ collection. Despite these efforts, Project
Gutenberg™ electronic works, and the medium on which they may
be stored, may contain “Defects,” such as, but not limited to,
incomplete, inaccurate or corrupt data, transcription errors, a
copyright or other intellectual property infringement, a defective or
damaged disk or other medium, a computer virus, or computer
codes that damage or cannot be read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except


for the “Right of Replacement or Refund” described in paragraph
1.F.3, the Project Gutenberg Literary Archive Foundation, the owner
of the Project Gutenberg™ trademark, and any other party
distributing a Project Gutenberg™ electronic work under this
agreement, disclaim all liability to you for damages, costs and
expenses, including legal fees. YOU AGREE THAT YOU HAVE NO
REMEDIES FOR NEGLIGENCE, STRICT LIABILITY, BREACH OF
WARRANTY OR BREACH OF CONTRACT EXCEPT THOSE
PROVIDED IN PARAGRAPH 1.F.3. YOU AGREE THAT THE
FOUNDATION, THE TRADEMARK OWNER, AND ANY
DISTRIBUTOR UNDER THIS AGREEMENT WILL NOT BE LIABLE
TO YOU FOR ACTUAL, DIRECT, INDIRECT, CONSEQUENTIAL,
PUNITIVE OR INCIDENTAL DAMAGES EVEN IF YOU GIVE
NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of receiving it,
you can receive a refund of the money (if any) you paid for it by
sending a written explanation to the person you received the work
from. If you received the work on a physical medium, you must
return the medium with your written explanation. The person or entity
that provided you with the defective work may elect to provide a
replacement copy in lieu of a refund. If you received the work
electronically, the person or entity providing it to you may choose to
give you a second opportunity to receive the work electronically in
lieu of a refund. If the second copy is also defective, you may
demand a refund in writing without further opportunities to fix the
problem.

1.F.4. Except for the limited right of replacement or refund set forth in
paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of damages.
If any disclaimer or limitation set forth in this agreement violates the
law of the state applicable to this agreement, the agreement shall be
interpreted to make the maximum disclaimer or limitation permitted
by the applicable state law. The invalidity or unenforceability of any
provision of this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the


Foundation, the trademark owner, any agent or employee of the
Foundation, anyone providing copies of Project Gutenberg™
electronic works in accordance with this agreement, and any
volunteers associated with the production, promotion and distribution
of Project Gutenberg™ electronic works, harmless from all liability,
costs and expenses, including legal fees, that arise directly or
indirectly from any of the following which you do or cause to occur:
(a) distribution of this or any Project Gutenberg™ work, (b)
alteration, modification, or additions or deletions to any Project
Gutenberg™ work, and (c) any Defect you cause.

Section 2. Information about the Mission of


Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new computers.
It exists because of the efforts of hundreds of volunteers and
donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project Gutenberg™’s
goals and ensuring that the Project Gutenberg™ collection will
remain freely available for generations to come. In 2001, the Project
Gutenberg Literary Archive Foundation was created to provide a
secure and permanent future for Project Gutenberg™ and future
generations. To learn more about the Project Gutenberg Literary
Archive Foundation and how your efforts and donations can help,
see Sections 3 and 4 and the Foundation information page at
www.gutenberg.org.
Section 3. Information about the Project
Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.

The Foundation’s business office is located at 809 North 1500 West,


Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up
to date contact information can be found at the Foundation’s website
and official page at www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission of
increasing the number of public domain and licensed works that can
be freely distributed in machine-readable form accessible by the
widest array of equipment including outdated equipment. Many small
donations ($1 to $5,000) are particularly important to maintaining tax
exempt status with the IRS.

The Foundation is committed to complying with the laws regulating


charities and charitable donations in all 50 states of the United
States. Compliance requirements are not uniform and it takes a
considerable effort, much paperwork and many fees to meet and
keep up with these requirements. We do not solicit donations in
locations where we have not received written confirmation of
compliance. To SEND DONATIONS or determine the status of
compliance for any particular state visit www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states where


we have not met the solicitation requirements, we know of no
prohibition against accepting unsolicited donations from donors in
such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot make


any statements concerning tax treatment of donations received from
outside the United States. U.S. laws alone swamp our small staff.

Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.

Section 5. General Information About Project


Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could be
freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose network of
volunteer support.

Project Gutenberg™ eBooks are often created from several printed


editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how
to subscribe to our email newsletter to hear about new eBooks.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like