Book description
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.
Publisher resources
Table of contents
- Foreword
- Preface
-
Part I. Foundations
-
1. Assert Yourself
- What Are Unit Tests Anyway?
- Create a Place to Play with Tests
- Write Your First Assertion
- Add a Descriptive Message
- Avoid Conditionals in Tests
- Describe Objects upon Failure
- Test for Equality
- Test Equality with Optionals
- Fudge Equality with Doubles and Floats
- Avoid Redundant Messages
- Choose the Right Assertion
- Key Takeaways
- Activities
- What’s Next?
-
2. Manage Your Test Life Cycles
- Make a New Place to Play
- Start from Test Zero
- Hook Up Tests to Production Code
- Examine Console Output
- Observe Object Life Cycles to Learn the Phases of a Test
- The Wrong Way to Reduce Duplicate Test Code
- Learn How XCTest Manages Test Cases
- Use setUp() and tearDown()
- Key Takeaways
- Activities
- What’s Next?
- 3. Measure Code Coverage and Add Tests
- 4. Take Control of Application Launch
-
5. Load View Controllers
- Make a New Place to Play
- Set Up a Storyboard-Based View Controller for Experiments
- Load a Storyboard-Based View Controller
- Set Up a XIB-Based View Controller for Experiments
- Load a XIB-Based View Controller
- Set Up a Code-Based View Controller for Experiments
- Load a Code-Based View Controller
- Key Takeaways
- Activities
- What’s Next?
-
6. Manage Difficult Dependencies
- Be Okay with Problem-Free Dependencies
- Identify Difficult Dependencies
- Create Boundaries to Isolate Dependencies
- Make a New Place to Play
- Add Backdoors to Singletons You Own
- Subclass and Override: A Legacy Code Technique
- Inject Instances Through Initializers or Properties
- Inject Closures to Make New Instances
- Key Takeaways
- Activities
- What’s Next?
-
1. Assert Yourself
-
Part II. iOS Testing Tips and Techniques
- 7. Testing Outlet Connections
- 8. Testing Button Taps (Using Actions)
- 9. Testing Alerts
- 10. Testing Navigation Between Screens
- 11. Testing UserDefaults (with Fakes)
- 12. Testing Network Requests (with Mocks)
- 13. Testing Network Responses (and Closures)
- 14. Testing Text Fields (and Delegate Methods)
- 15. Testing Table Views
- 16. Testing View Appearance (with Snapshots)
-
Part III. Using Your New Power
-
17. Unleash the Power of Refactoring
- What Is Refactoring?
- Lay Out the Views for Our Practice App
- Add the Code to Our Practice App
- 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?
- 18. Refactoring: Moving to MVVM
-
19. 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?
-
20. 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?
-
17. Unleash the Power of Refactoring
- Bibliography
Product information
- Title: iOS Unit Testing by Example
- Author(s):
- Release date: June 2020
- Publisher(s): Pragmatic Bookshelf
- ISBN: 9781680507973
You might also like
book
Pro iOS Testing: XCTest Framework for UI and Unit Testing
Discover what tools there are for unit testing in iOS, and how to work in a …
video
Testing in Xcode: Unit Testing, UI Testing, and Code Coverage
Learn automatic testing in Xcode. Start by creating a simple test app that intentionally crashes in …
book
Modularizing Legacy Projects Using TDD: Test-Driven Development with XCTest for iOS
Improve current or new projects with top notch testability and maintainability. Writing tests improves the design …
video
Practical Test-Driven Development with Xcode and Swift: Write Modularized Code for TDD with XCTest and UIKit
Write better, more robust, and well-architected code. Then put tests on CI with every run, so …