Atnaujinkite slapukų nuostatas

iOS Unit Testing by Example: XCTest Tips and Techniques Using Swift [Minkštas viršelis]

4.48/5 (79 ratings by Goodreads)
  • Formatas: Paperback / softback, 300 pages, aukštis x plotis x storis: 234x191x19 mm, weight: 662 g
  • Išleidimo metai: 10-Sep-2020
  • Leidėjas: The Pragmatic Programmers
  • ISBN-10: 1680506811
  • ISBN-13: 9781680506815
Kitos knygos pagal šią temą:
  • Formatas: Paperback / softback, 300 pages, aukštis x plotis x storis: 234x191x19 mm, weight: 662 g
  • Išleidimo metai: 10-Sep-2020
  • Leidėjas: The Pragmatic Programmers
  • ISBN-10: 1680506811
  • ISBN-13: 9781680506815
Kitos knygos pagal šią temą:

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.

Foreword xv
Preface xvii
Part I Foundations
1 Assert Yourself
3(16)
What Are Unit Tests Anyway?
4(1)
Create a Place to Play with Tests
4(2)
Write Your First Assertion
6(2)
Add a Descriptive Message
8(1)
Avoid Conditionals in Tests
9(1)
Describe Objects upon Failure
10(1)
Test for Equality
11(1)
Test Equality with Optionals
12(1)
Fudge Equality with Doubles and Floats
13(1)
Avoid Redundant Messages
14(1)
Choose the Right Assertion
15(1)
Key Takeaways
16(1)
Activities
17(1)
What's Next?
18(1)
2 Manage Your Test Life Cycles
19(16)
Make a New Place to Play
19(2)
Start from Test Zero
21(1)
Hook Up Tests to Production Code
22(1)
Examine Console Output
23(3)
Observe Object Life Cycles to Learn the Phases of a Test
26(2)
The Wrong Way to Reduce Duplicate Test Code
28(2)
Learn How XCTest Manages Test Cases
30(1)
Use setup() and teardown()
31(2)
Key Takeaways
33(1)
Activities
33(1)
What's Next?
34(1)
3 Measure Code Coverage and Add Tests
35(18)
Make a New Place to Play
35(1)
Enable Code Coverage
36(1)
Examine Code Coverage Results
37(2)
Drill into a Partially Covered Line
39(1)
Add Tests for Existing Code
40(2)
Cover a Conditional
42(3)
Cover a Loop
45(1)
Cover Statements in a Sequence
46(3)
Avoid Percentage Targets, Embrace Forward Movement
49(1)
Key Takeaways
49(1)
Activities
50(1)
What's Next?
50(3)
4 Take Control of Application Launch
53(8)
Make a New Place to Play
53(1)
Observe the Default Behavior
54(1)
Learn About the Test Launch Sequence
55(1)
Bypass the Normal App Delegate
55(2)
Put Up with the Initial View Controller
57(1)
Tweak Your Testing App Delegate
57(1)
Key Takeaways
58(1)
Activities
58(1)
What's Next?
59(2)
5 Load View Controllers
61(12)
Make a New Place to Play
61(1)
Set Up a Storyboard-Based View Controller for Experiments
62(2)
Load a Storyboard-Based View Controller
64(2)
Set Up a XIB-Based View Controller for Experiments
66(1)
Load a XIB-Based View Controller
67(1)
Set Up a Code-Based View Controller for Experiments
68(1)
Load a Code-Based View Controller
69(2)
Key Takeaways
71(1)
Activities
71(1)
What's Next?
72(1)
6 Manage Difficult Dependencies
73(22)
Be Okay with Problem-Free Dependencies
73(2)
Identify Difficult Dependencies
75(2)
Create Boundaries to Isolate Dependencies
77(1)
Make a New Place to Play
77(3)
Add Backdoors to Singletons You Own
80(3)
Subclass and Override: A Legacy Code Technique
83(2)
Inject Instances Through Initializers or Properties
85(3)
Inject Closures to Make New Instances
88(2)
Key Takeaways
90(1)
Activities
91(1)
What's Next?
91(4)
Part II iOS Testing Tips and Techniques
7 Testing Outlet Connections
95(6)
Make a New Place to Play
95(1)
Test Outlet Connections
96(1)
Check the Effectiveness of Failure Messages
97(1)
Key Takeaways
98(1)
Activities
98(1)
What's Next?
99(2)
8 Testing Button Taps (Using Actions)
101(6)
Make a Place to Play with a Button
101(1)
Test Button Taps
102(2)
Make a Test Helper for Button Taps
104(1)
Key Takeaways
105(1)
Activities
105(1)
What's Next?
106(1)
9 Testing Alerts
107(8)
Make a New Place to Play
107(1)
Add the Helper Framework to the Project
108(2)
Test Alerts Using the Alert Verifier
110(2)
Move the SUT into the Test Fixture
112(1)
Add Tests for Alert Buttons
112(1)
Key Takeaways
113(1)
Activities
114(1)
What's Next?
114(1)
10 Testing Navigation Between Screens
115(14)
Make a New Place to Play
115(1)
Set Up Code-Based Navigation
116(2)
Set Up Segue-Based Navigation
118(1)
Test Code-Based Push Navigation
119(3)
Test Code-Based Modal Presentation
122(2)
Test Segue-Based Push Navigation
124(2)
Test Segue-Based Modal Navigation
126(1)
Key Takeaways
127(1)
Activities
128(1)
What's Next?
128(1)
11 Testing UserDefaults (with Fakes)
129(10)
Make a New Place to Play
129(1)
Isolate UserDefaults with Dependency Injection
130(1)
Extract a Protocol to Support Test Doubles
131(2)
Make a Fake Object
133(1)
Test UserDefaults
134(3)
Key Takeaways
137(1)
Activities
137(1)
What's Next?
138(1)
12 Testing Network Requests (with Mocks)
139(14)
Make a New Place to Play
139(2)
Isolate URLSession with Dependency Injection
141(1)
Extract a URLSession Protocol for Test Doubles
141(2)
Make a Test Spy
143(1)
Design the Test Case
144(2)
Promote the Test Spy into a Mock Object
146(2)
Improve Mock Object Reporting
148(3)
Key Takeaways
151(1)
Activities
151(1)
What's Next?
152(1)
13 Testing Network Responses (and Closures)
153(16)
Make a New Place to Play
153(2)
Parse the Response
155(3)
Start with a Fresh Test Spy
158(1)
Design the Test Case
159(2)
Test Asynchronous Code
161(2)
Keep Asynchronous Code in Its Closure
163(1)
Test an Error Scenario
164(3)
Key Takeaways
167(1)
Activities
168(1)
What's Next?
168(1)
14 Testing Text Fields (and Delegate Methods)
169(16)
Make a Place to Play
169(3)
Test the Outlets
172(1)
Test Attributes and Wrangle UIKlt Descriptions
173(4)
Test Delegate Methods
177(3)
Test Input Focus
180(3)
Key Takeaways
183(1)
Activities
184(1)
What's Next?
184(1)
15 Testing Table Views
185(8)
Make a Place to Play
185(2)
Test Table Views
187(4)
Key Takeaways
191(1)
Activities
191(1)
What's Next?
192(1)
16 Testing View Appearance (with Snapshots)
193(16)
Make a Place to Play
193(3)
Add FBSnapshotTestCase to a Test Target
196(2)
Set the Location for Reference Images
198(1)
Write a Snapshot Test
199(2)
See the Difference in a Snapshot Failure
201(2)
Manage Your Snapshot Tests
203(2)
Key Takeaways
205(1)
Activities
206(1)
What's Next?
206(3)
Part III Using Your New Power
17 Unleash the Power of Refactoring
209(46)
What Is Refactoring?
209(1)
Lay Out the Views for Our Practice App
210(3)
Add the Code to Our Practice App
213(7)
Replace the Difficult Dependency with a Mock Object
220(3)
Write the First Tests of the Change Password View Controller
223(4)
Test the Cancel Button
227(2)
Test the Submit Button
229(11)
Test the Text Field Delegate Method
240(2)
Refactor to Break Up a Long Function
242(3)
Extract a Method with Parameters
245(6)
Clean Up a Few More Places
251(2)
Key Takeaways
253(1)
What's Next?
254(1)
18 Refactoring: Moving to MWM
255(18)
What Is MWM?
255(1)
Replace String Literals to Use a View Model
256(3)
Overwrite Storyboard Labels
259(1)
Respond to Changes in the View Model
260(8)
Move Logic into the View Model
268(3)
Key Takeaways
271(1)
What's Next?
272(1)
19 Refactoring: Moving to MVP
273(24)
What Is MVP?
273(1)
Set Up the MVP Types
274(1)
Extract Methods into the View Commands Protocol
275(4)
Move a Function into the Presenter
279(2)
Remove the didSet Observer
281(3)
Use Refactoring Principles to Reparent a Swift Type
284(2)
Move Several Functions to the Presenter
286(2)
Extract Password Validation into Its Own Type
288(5)
Finish Up the Refactoring to MVP
293(2)
Key Takeaways
295(1)
What's Next?
296(1)
20 Test-Driven Development Beckons to You
297(24)
What Is TDD?
297(2)
Make a New Place to Play with TDD
299(1)
Define the Requirements of the Tlme-of-Day Greeter
299(1)
Design the First Failing Test with Bare Production Code
300(3)
Make the First Test Pass with "Good Morning"
303(1)
Refactor the First Test to Make It More Expressive
303(2)
Repeat the TDD Steps for the Second Test
305(2)
Add Tests to Expand "Good Afternoon"
307(1)
Implement "Good Evening"
308(1)
Step Back to Refactor the Method as a Whole
309(5)
Add the Name to the Greeting
314(3)
Key Takeaways
317(1)
What's Next?
318(3)
Bibliography 321(2)
Index 323
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, https: //qualitycoding.org focuses on iOS unit testing practices.