Test-Driven iOS Development
Graham Lee
![Upper Saddle River NJ Boston Indianapolis San Francisco New York Toronto - photo 1](/uploads/posts/book/42204/graphics/pub.jpg)
Upper Saddle River, NJ Boston Indianapolis San Francisco
New York Toronto Montreal London Munich Paris Madrid
Cape Town Sydney Tokyo Singapore Mexico City
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 publisher 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 omissions. 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 purchases 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
For sales outside the United States, please contact:
International Sales
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 reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use material 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 printed in the United States on recycled paper at R.R. Donnelley in Crawfordsville,
Indiana.
First printing, April 2012
Editor-in-Chief
Mark Taub
Senior Acquisitions Editor
Trina MacDonald
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
Developers Library
Essential References for Programming Professionals
Developers 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 Developers Library are written by expert technology practitioners who are especially skilled at organizing and presenting information in a way thats 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
Developers Library books are available at most retail and online bookstores, as well as by subscription from Safari Books Online at safari.informit.com
Developers Library
informit.com/devlibrary
This book is for anyone who has ever shipped a bug. Youre
in great company.
Preface
My experience of telling other developers about test-driven development for Objective-C came about almost entirely by accident. I was scheduled to talk at a conference on a different topic, where a friend of mine was talking on TDD. His wife had chosen (I assume thats how it works; Im no expert) that weekend to give birth to their twins, so Chuckwho commissioned the book you now hold in your handsasked me if I wouldnt mind giving that talk, too. Thus began the path that led ultimately to the year-long project of creating this book.
Its usually the case that reality is not nearly as neat as the stories we tell each other about reality. In fact, I had first encountered unit tests a number of years previously. Before I was a professional software engineer, I was a tester for a company whose product was based on GNUstep (the Free Software Foundations version of the Cocoa libraries for Linux and other operating systems). Unit testing, I knew then, was a way to show that little bits of a software product worked properly, so that hopefully, when they were combined into big bits of software, those big bits would work properly, too.
I took this knowledge with me to my first programming gig, as software engineer working on the Mac port of a cross-platform security product. (Another simplificationI had, a few years earlier, taken on a six-week paid project to write a LISP program. Weve all done things were not proud of.) While I was working this job, I went on a TDD training course, run by object-oriented programming conference stalwart Kevlin Henney, editor of 97 Things Every Programmer Should Know, among other things. It was here that I finally realized that the point of test-driven development was to make me more confident about my code, and more confident about changing my code as I learned more. The time had finally arrived where I understood TDD enough that I could start learning from my own mistakes, make it a regular part of my toolbox, and work out what worked for me and what didnt. After a few years of that, I was in a position where I could say yes to Chucks request to give the talk.
Its my sincere hope that this book will help you get from discovering unit testing and test-driven development to making it a regular part of how you work, and that you get there in less time than the five years or so it took me. Plenty of books have been written about unit testing, including by the people who wrote the frameworks and designed the processes. These are good books, but they dont have anything specifically to say to Cocoa 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, yesthe 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 Ive covered some of those differences here, I decided to focus almost exclusively on the capabilities Apple supplies in Xcode and the OCUnit framework. The reason is simply one of applicability; anyone whos interested in trying out unit tests or TDD can get on straight away with just the knowledge in this book, the standard tools, and a level of determination. If you find aspects of it lacking or frustrating, you can, of course, investigate the alternatives or even write your ownjust remember to test it!