Pages: 358 Published: June 2020 ISBN: 9781680506815
In Print
iOS Unit Testing by Example
XCTest Tips and Techniques Using Swift
by Jon Reid
Fearlessly change the design of your iOS code with solid unit tests. Use
Xcode’s built-in test framework XCTest and Swift to get rapid feedback
on all your code — including legacy code. Learn the tricks and
techniques of testing all iOS code, especially view controllers
(UIViewControllers), which are critical to iOS apps. Learn to isolate
and replace dependencies in legacy code written without tests. Practice
safe refactoring that makes these tests possible, and watch all your
changes get verified quickly and automatically. Make even the boldest
code changes with complete confidence.
Manual code and UI testing get slower the deeper your navigation
hierarchy goes. It can take several taps just to reach a particular
screen, never mind the actual workflow tests. Automatic unit testing
offers such rapid feedback that it can change the rules of development.
Bring testing to iOS development, even for legacy code. Use XCTest to
write unit tests in Swift for all your code.
iOS developers typically reserve unit tests for their model classes
alone. But that approach skips most of the code common to iOS apps,
especially with UIViewControllers. Learn how to unit test these view
controllers to expand your unit testing possibilities. Since good unit
tests form the bedrock for safe refactoring, you’re empowered to make
bold changes. Learn how to avoid the most common mistakes Swift
programmers make with the XCTest framework. Use code coverage to find
holes in your test suites. Learn how to identify hard dependencies.
Reshape the design of your code quickly, with less risk and less fear.
Production is complete. Now it’s on to layout and the printer.
2020/04/16
B8.0
• Added chapter 20.
• Removed outdated method of suppressing root view controller from chapter 4.
• Updated FBSnapshotTestCase to 6.2.0, use new FBSnapshotVerifyView-Controller() function.
• Updated description of how to set the location for snapshot reference images.
• Updated images to latest Xcode.
• Content complete and headed to production.
2019/11/13
B7.0
*Added chapter 19, Refactoring: Moving to Model View Presenter (MVP).
Replace the Difficult Dependency with a Mock Object
Write the First Tests of the Change Password View Controller
Test the Cancel Button
Test the Submit Button
Test the Text Field Delegate Method
Refactor to Break Up a Long Function
Extract a Method with Parameters
Clean Up a Few More Places
Key Takeaways
What’s Next?
Refactoring: Moving to MVVM
What Is MVVM?
Replace String Literals to Use a View Model
Overwrite Storyboard Labels
Respond to Changes in the View Model
Move Logic into the View Model
Key Takeaways
What’s Next?
Refactoring: Moving to MVP
What Is MVP?
Set Up the MVP Types
Extract Methods into the View Commands Protocol
Move a Function into the Presenter
Remove the didSet Observer
Use Refactoring Principles to Reparent a Swift Type
Move Several Functions to the Presenter
Extract Password Validation into Its Own Type
Finish Up the Refactoring to MVP
Key Takeaways
What’s Next?
Test-Driven Development Beckons to You
What Is TDD?
Make a New Place to Play with TDD
Define the Requirements of the Time-of-Day Greeter
Design the First Failing Test with Bare Production Code
Make the First Test Pass with “Good Morning”
Refactor the First Test to Make It More Expressive
Repeat the TDD Steps for the Second Test
Add Tests to Expand “Good Afternoon”
Implement “Good Evening”
Step Back to Refactor the Method as a Whole
Add the Name to the Greeting
Key Takeaways
What’s Next?
Author
Jon Reid is a leading expert in unit testing and Test-Driven
Development for iOS development. He has been practicing TDD on Apple
platforms since 2001. His blog Quality
Coding focuses on iOS unit testing
practices.