Test-Driven Development in Swift by Gio Lodi
Author:Gio Lodi
Language: eng
Format: epub
ISBN: 9781484270028
Publisher: Apress
© Gio Lodi 2021
G. LodiTest-Driven Development in Swifthttps://doi.org/10.1007/978-1-4842-7002-8_9
9. Testing JSON Decoding
Gio Lodi1
(1)Mount Martha, VIC, Australia
How do you use tests to drive writing logic to decode JSON data into a model object?
By feeding the tests a predefined JSON input and verify the decoding produces a value matching it.
Swiftâs Decodable protocol offers a type-safe way to describe how to transform Data, such as a JSON in the body of an HTTP response, into an object or value type. With Decodable doing most of the heavy lifting, is it worth using tests to drive the implementation of JSON decoding logic?
When the JSON object has keys and values with names and types that map directly to Swift, there is no real logic you need to write to decode it. In such a case, you may feel comfortable writing the source code directly, without the help of a unit test. If the JSON object is elaborate or if you want to decode it into a Swift type with a different structure, then a test will help you along the way.
Choosing whether to deploy TDD to implement JSON decoding logic is a matter of tradeoffs. Is the extra time youâre spending writing the test worth the feedback it gives you? Once the test is in place, will it guard against regression that may otherwise go unnoticed?
To answer those questions, you first need to know how to write this kind of tests. In this chapter, weâll explore two different techniques to write tests for JSON decoding and reflect further on the return on investment of applying TDD to this part of the appâs implementation.
Weâre moving along steadily in making our app fetch the menu from a remote API. In the previous chapters, we built the view part of this new functionality. Itâs now time to take one step further down the stack, coding the conversion from the raw Data a network request would return into the MenuItem domain model.
Swift provides a convenient way to decode Data into a different type via the Decodable protocol. Our job is to conform MenuItem to it and verify the decoding is successful.
As part of your application development agreement with Alberto, youâre also building his backend service. You chose to use a REST API that returns a JSON menu in the following form:1[
{
"name": "spaghetti carbonara",
"category": "pasta",
"spicy": false
},
{
"name": "penne all'arrabbiata",
"category": "pasta",
"spicy": true
}
]
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Windows Terminal Tips, Tricks, and Productivity Hacks by Will Fuqua(1402)
Augmented Reality with Unity AR Foundation by Jonathan Linowes(1180)
Sketching User Experiences by Buxton Bill(590)
Object-Role Modeling Fundamentals: A Practical Guide to Data Modeling with ORM by Terry Halpin(563)
Beginning AutoCAD 2020 Exercise Workbook by Cheryl R. Shrock(539)
You Donât Know JS: Up & Going by Kyle Simpson(519)
Adobe InDesign for Absolute Beginner: Tips and Techniques to Creating Professional Books and Magazines for Online and Desktop Publishing using InDesign by Scott Walter & Scott Walter(461)
AI 2041: Ten Visions for Our Future by Kai-Fu Lee & Chen Qiufan(454)
SketchUp For Dummies by Bill Fane & Mark Harrison & Josh Reilly(453)
Windows 10: The Missing Manual by Pogue David(445)
100 Principles of Game Design by unknow(441)
Advanced Game Design by Michael Sellers(429)
The Rainbow Stack: A Casual Guide to UX Design by Jawara Joseph(411)
Research Methods in Human-Computer Interaction by unknow(411)
Mastering InDesign Templates by Murphy Monica & Knorr Mancini Margot(409)
Hands-On Data Visualization by Jack Dougherty(407)
Photoshop Elements 12 All-in-One For Dummies by Barbara Obermeier & Ted Padova(402)
TensorFlow 2.0 Computer Vision Cookbook by Jesus Martinez(384)
Connected, Intelligent, Automated: The Definitive Guide to Digital Transformation and Quality 4.0 by Radziwill N. M(384)