ptg7913098 www.it-ebooks.info ptg7913098 Test-Driven iOS Development www.it-ebooks.info ptg7913098 informit.com/devlibrary Developer’s Library ESSENTIAL REFERENCES FOR PROGRAMMING PROFESSIONALS Developer’s Library books are designed to provide practicing programmers with unique, high-quality references and tutorials on the programming languages and technologies they use in their daily work. All books in the Developer’s Library are written by expert technology practitioners who are especially skilled at organizing and presenting information in a way that’s useful for other programmers. Key titles include some of the best, most widely acclaimed books within their topic areas: PHP & MySQL Web Development Luke Welling & Laura Thomson ISBN 978-0-672-32916-6 MySQL Paul DuBois ISBN-13: 978-0-672-32938-8 Linux Kernel Development Robert Love ISBN-13: 978-0-672-32946-3 Python Essential Reference David Beazley ISBN-13: 978-0-672-32862-6 Programming in Objective-C Stephen G. Kochan ISBN-13: 978-0-321-56615-7 PostgreSQL Korry Douglas ISBN-13: 978-0-672-33015-5 Developer’s Library books are available at most retail and online bookstores, as well as by subscription from Safari Books Online at safari.informit.com Developer’s Library www.it-ebooks.info ptg7913098 Test-Driven iOS Development Graham Lee Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • To r o n t o • Montreal • London • Munich • Paris • Madrid Cape Town • Sydney • To k y o • Singapore • Mexico City www.it-ebooks.info ptg7913098 Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publish- er was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omis- sions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk pur- chases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside the United States, please contact: International Sales international@pearsoned.com Visit us on the Web: informit.com/aw Library of Congress Cataloging-in-Publication Data is on file Copyright © 2012 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited repro- duction, storage in a retrieval system, or transmission in any form or by any means, elec- tronic, mechanical, photocopying, recording, or likewise. To obtain permission to use materi- al from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290. ISBN-13: 978-0-32-177418-7 ISBN-10: 0-32-177418-3 Text p ri nt ed in the Un it ed States on r ec yc le d paper a t R .R . Donnelley in Crawfordsville, Indiana. First printing, April 2012 Editor-in-Chief Mark Taub Senior Acquisitions Editor Trina Ma cD on al d Managing Editor Kristy Hart Project Editor Andy Beaster Copy Editor Barbara Hacha Indexer Tim Wright Proofreader Paula Lowell Technical Reviewers Richard Buckle Patrick Burleson Andrew Ebling Alan Francis Rich Wardwell Publishing Coordinator Olivia Basegio Book Designer Gary Adair Compositor Gloria Schurick www.it-ebooks.info ptg7913098 ❖ This book is for anyone who has ever shipped a bug.You’re in great company. ❖ www.it-ebooks.info ptg7913098 This page intentionally left blank www.it-ebooks.info ptg7913098 Contents at a Glance Preface xii 1 About Software Testing and Unit Testing 1 2 Techniques for Test-Driven Development 13 3 How to Write a Unit Test 23 4 Tools for Testing 35 5 Test-Driven Development of an iOS App 59 6 The Data Model 67 7 Application Logic 87 8 Networking Code 113 9 View Controllers 127 10 Putting It All Together 171 11 Designing for Test-Driven Development 201 12 Applying Test-Driven Development to an Existing Project 209 13 Beyond Today’s Test-Driven Development 215 Index 221 www.it-ebooks.info ptg7913098 Table of Contents Dedication v Preface xii Acknowledgments xiv About the Author xiv 1 About Software Testing and Unit Testing 1 What Is Software Testing For? 1 Who Should Test Software? 2 When Should Software Be Tested? 6 Examples of Testing Practices 7 Where Does Unit Testing Fit In? 7 What Does This Mean for iOS Developers? 11 2 Techniques for Test-Driven Development 13 Test F irst 13 Red, Green, Refactor 15 Designing a Test-Driven App 18 More on Refactoring 19 Ya Ain’t Gonna Need It 19 Test ing Bef ore , During, and After Coding 21 3 How to Write a Unit Test 23 The Requirement 23 Running Code with Known Input 24 Seeing Expected Results 26 Verifying the Results 26 Making the Tests More Readable 28 Organizing Multiple Tests 29 Refactoring 32 Summary 34 www.it-ebooks.info ptg7913098 ix Contents 4 Tools for Testing 35 OCUnit with Xcode 35 Alternatives to OCUnit 46 Google Toolkit for Mac 46 GHUnit 47 CATCH 48 OCMock 50 Continuous Integration 52 Hudson 53 CruiseControl 57 Summary 58 5 Test-Driven Development of an iOS App 59 Product Goal 59 Use Cases 60 Plan of Attack 63 Getting Started 64 6 The Data Model 67 Topi cs 67 Questions 73 People 75 Connecting Questions to Other Classes 76 Answers 81 7 Application Logic 87 Plan of Attack 87 Creating a Question 88 Building Questions from JSON 102 8 Networking Code 113 NSURLConnection Class Design 113 StackOverflowCommunicator Implementation 114 Conclusion 125 www.it-ebooks.info [...]... 213 www.it-ebooks.info 209 Contents 13 Beyond Today’s Test-Driven Development Expressing Ranges of Input and Output Behavior-Driven Development 215 216 Automatic Test Case Generation 217 Automatically Creating Code to Pass Tests Conclusion Index 220 221 www.it-ebooks.info 215 219 xi Preface My experience of telling other developers about test-driven development for ObjectiveC came about almost entirely... confident about its behavior.The next chapter will introduce you to the concepts behind test-driven development: concepts that will be used throughout the rest of the book www.it-ebooks.info 11 This page intentionally left blank www.it-ebooks.info 2 Techniques for Test-Driven Development Youa have seenthe software development process: You can test your own code and have in Chapter 1, “About Software... Ship It! 171 189 199 11 Designing for Test-Driven Development 201 Design to Interfaces, Not Implementations 201 Tell, Don’t Ask 203 Small, Focused Classes and Methods Encapsulation 204 205 Use Is Better Than Reuse Testing Concurrent Code 205 206 Don’t Be Cleverer Than Necessary 207 Prefer a Wide, Shallow Inheritance Hierarchy Conclusion 208 208 12 Applying Test-Driven Development to an Existing Project... mention the developer tools team at Apple, who have done more than anyone else to put unit testing onto the radar (if you’ll pardon the pun) of iOS developers the world over Kevlin Henney was the person who, more than anyone else, showed me the value of test-driven development; thank you for all those bugs that I didn’t write And finally, Freya has been supportive and understanding of the strange hours... known as beta testers) 1 In fact, many software projects, including iOS apps, are still managed this way This fact shouldn’t get in the way of your believing that the waterfall model is an obsolete historical accident www.it-ebooks.info Who Should Test Software? Requirements Specification Development Test Deployment Figure 1.1 The phases of development in the waterfall software project management process... advantage that unit testing brings to developers of iOS apps is that a lot of benefit can be reaped for little cost Because many of the hundreds of thousands of apps in the App Store are produced by micro-ISVs, anything that can improve the quality of an app without requiring much investment is a good thing.The tools needed to add unit tests to an iOS development project are free In fact, as described... rapidly get a fix out is disastrous You will find that as you get more comfortable with test-driven development writing the tests and the code together—you get faster at writing code because thinking about the code’s design and the conditions it will need to cope with become second nature.You will soon find that writing test-driven code, including its tests, takes the same time that writing the code alone... Touch developers By providing examples in the Objective-C language, using Xcode and related tools, and working with the Cocoa idioms, I hope to make the principles behind test-driven development more accessible and more relevant to iOS developers Ah, yes—the tools.There are plenty of ways to write unit tests, depending on different features in any of a small hoard of different tools and frameworks Although... Objective-C)—have refined their techniques, and created new ways to incorporate unit testing into software development. This chapter is about technique—and using unit tests to improve your efficiency as a developer Test First The practice developed by Extreme Programming aficionados is test-first or test-driven development, which is exactly what it sounds like: Developers are encouraged to write tests before... each of the problems in creating your app twice, with a large gap in www.it-ebooks.info 14 Chapter 2 Techniques for Test-Driven Development between each go Remembering what you were thinking about when you wrote any particular group of tests a few months earlier would not be an easy task So test-driven developers do not write all the tests first, but they still don’t write code before they’ve written the . 171 11 Designing for Test-Driven Development 201 12 Applying Test-Driven Development to an Existing Project 209 13 Beyond Today’s Test-Driven Development 215 Index. 1 2 Techniques for Test-Driven Development 13 3 How to Write a Unit Test 23 4 Tools for Testing 35 5 Test-Driven Development of an iOS App 59 6 The Data