Rails Test Prescriptions

Rails Test Prescriptions

What readers are saying about Rails Test Prescriptions This a must-have book for those new to testing on a team that thinks, "We don’t have time for testing," and for experienced developers look- ing to round out their testing skills. If you want to write better code, deploy with confidence, and accelerate your team’s velocity, you should read this book! John McCaffrey Rails Developer/Project Manager, Railsperformance.blogspot.com Rails Test Prescriptions presents a nuanced and unbiased overview of the tools and techniques professionals use to test their Rails apps every day. A must-read for any Rails developer, whether you’ve never written a single test or you’ve written thousands. David Chelimsky Senior Software Engineer, DRW Trading Rails Test Prescriptions is a great resource for anyone interested in getting better at testing Rails applications. New readers will find many helpful guides, and experienced readers will discover many lesser- known tips and tricks. Nick Gauthier Developer, SmartLogic Solutions If you are comfortable working with Rails, yet have no experience writ- ing tests for it, this book is an excellent resource for getting up to speed on the most successful tools used to test drive your develop- ment. Adam Williams (@aiwilliams) Noel has dispensed a fantastic collection of prescriptions for all kind of testing maladies. Whether you are a budding intern, or a highly specialized surgeon, this book will provide you with the information you need to improve your testing health. Christopher Redinger Principal, Relevance, Inc. Testing is a given in the Rails world, but the varied options can be daunting if you are just starting to learn the framework. Noel provides a solid tour of the options and techniques for testing a Rails applica- tion that will help guide you past some of the initial dark corners. If you are entering the world of Ruby on Rails, I’d recommend keeping a copy of Rails Test Prescriptions at hand. Corey Haines Software Journeyman Rails TestPrescriptions Keeping Y o u r Application Healthy Noel Rappin The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas Many of the designations used by manufacturers and sellers to distinguish their prod- ucts are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://www.pragprog.com. The team that produced this book includes: Editor: Colleen Toporek Indexing: Potomac Indexing, LLC Copy edit: Kim W i m p s e t t Production: Janet Furlow Customer support: Ellie Callahan International: Juliet Benda Copyright © 2010 Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmit- ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-10: 1-934356-64-6 ISBN-13: 978-1-934356-64-7 Printed on acid-free paper. P1.0 printing, February 2011 V e r s i o n : 2011-2-14 Contents I Getting Started with T e s t i n g in Rails 12 1 The Goals of Automated Developer T e s t i n g 13 1.1 A Testing Fable . 13 1.2 Who Are Y o u ? . 15 1.3 The Power of Testing First . 16 1.4 What Is TDD Good For? . 17 1.5 When TDD Needs Some Help . 19 1.6 Coming Up Next... 20 1.7 Acknowledgments . 22 2 The Basics of Rails T e s t i n g 24 2.1 What’s a Test? . 24 2.2 What Goes in a Test? . 26 2.3 Setup and Teardown . 29 2.4 What Can Y o u Test in Rails? . 32 2.5 What Happens When Tests Run? . 34 2.6 Running the Rails Tests . 36 2.7 More Info: Getting Data into the Test . 38 2.8 Beyond the Basics . 41 3 W r i t i n g Y o u r First T e s t s 42 3.1 The First Test-First . 44 3.2 The First Refactor . 47 3.3 More Validations . 49 3.4 Security Now! . 53 3.5 Applying Security . 55 3.6 Punishing Miscreants . 56 3.7 Road Map . 60 CONTENTS 8 4 TDD, Rails Style 61 4.1 Now for a V i e w Test . 61 4.2 Testing the Project V i e w : A Cascade of Tests . 64 4.3 So Far, So Good . 70 II T e s t i n g Application Data 71 5 T e s t i n g Models with Rails Unit T e s t s 72 5.1 What’s Available in a Model Test . 72 5.2 What to Test in a Model Test . 74 5.3 OK, Funny Man, What Is a Good Model Test Class? 74 5.4 Asserting a Difference, or Not . 76 5.5 Testing Active Record Finders . 77 5.6 Coming Up Next . 80 6 Creating Model T e s t Data with Fixtures and Factories 81 6.1 Defining Fixture Data . 81 6.2 Loading Fixture Data . 84 6.3 Why Fixtures Are a Pain . 85 6.4 Using Factories to Fix Fixtures . 86 6.5 Data Factories . 87 6.6 Installing factory_girl . 87 6.7 Creating and Using Simple Factories . 88 6.8 Sequencing for Unique Attributes . 90 6.9 Freedom of Association . 91 6.10 Factories of the W o r l d Unite . 93 6.11 Managing Date and TimeData . 95 6.12 Model Data Summary . 99 7 Using Mock Objects 101 7.1 What’s a Mock Object? . 101 7.2 Stubs . 103 7.3 Stubs with Parameters . 108 7.4 Mock, Mock, Mock . 112 7.5 Mock Objects and Behavior-Driven Development . 114 7.6 Mock Dos and Mock Don’ts . 117 7.7 Comparing Mock Object Libraries . 118 7.8 Mock Object Summary . 126 Report erratum this copy is (P1.0 printing, Febr uar y 2011) CONTENTS 9 III T e s t i n g User-Facing Layers 127 8 T e s t i n g Controllers with Functional T e s t s 128 8.1 What’s Available in a Controller Test? . 128 8.2 What to Test . 129 8.3 Simulating a Controller Call . 130 8.4 Testing Controller Response . 133 8.5 Testing Returned Data . 134 8.6 Testing Routes . 137 8.7 Coming Up . 138 9 T e s t i n g Views 139 9.1 The Goals of V i e w Testing . 139 9.2 Keys to Successful V i e w Testing . 140 9.3 Using assert_select . 141 9.4 Testing Outgoing Email . 146 9.5 Testing Helpers . 148 9.6 Testing Block Helpers . 150 9.7 Using assert_select in Helper Tests . 151 9.8 How Much TimeShould Y o u Spend on Helpers? . 153 9.9 When to V i e w Test . 153 10 T e s t i n g JavaScript and Ajax 155 10.1 First Off, RJS . 156 10.2 Testing JavaScript from Rails with Jasmine . 158 10.3 Getting Started with Jasmine . 158 10.4 Running Jasmine Tests . 159 10.5 W r i t i n g Jasmine Tests . 161 10.6 Integrating Jasmine with Dynamic Rails . 165 IV T e s t i n g Framework Extensions 168 11 W r i t e Cleaner T e s t s with Shoulda and Contexts 169 11.1 Contexts . 170 11.2 Basics of Shoulda . 173 11.3 Single Assertion Testing . 173 11.4 Shoulda Assertions . 175 11.5 Shoulda One-Liners . 176 11.6 W r i t i n g Y o u r Own Shoulda Matcher . 179 11.7 Single-Line Test Tools . 183 11.8 When to Use Shoulda . 185 Report erratum this copy is (P1.0 printing, Febr uar y 2011) CONTENTS 10 12 RSpec 186 12.1 Getting Started with RSpec . 187 12.2 RSpec in Ten Minutes . 189 12.3 RSpec and Rails . 199 12.4 Running RSpec . 209 12.5 RSpec in Practice . 209 12.6 Creating Y o u r Own Matchers . 211 12.7 Summarizing RSpec . 213 V T e s t i n g Everything All T o g e t h e r 214 13 T e s t i n g W o r k fl o w with Integration T e s t s 215 13.1 What to Test in an Integration Test . 216 13.2 What’s Available in an Integration Test? . 216 13.3 Simulating Multipart Interaction . 218 13.4 Simulating a Multiuser Interaction . 220 13.5 When to Use Integration Tests . 223 14 W r i t e Better Integration T e s t s with W e b r a t and Capybara 224 14.1 Installing W e b r a t and Capybara . 225 14.2 Using the Acceptance Testing Rodents .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    352 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us