1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu The RSpec Book: Behaviour Driven Development with Rspec, Cucumber, and Friends pptx

426 1,8K 3

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 426
Dung lượng 7 MB

Nội dung

Download from Wow! eBook <www.wowebook.com> www.it-ebooks.info What Readers Are Saying About The RSpec Book: Behaviour-Driven Development with RSpec, Cucumber, and Friends The RSpec Book is a fantastic introduction to all things BDD. It goes much deeper than just testing to provide you with the right tools you need to fully embrace the ideas that the framework has baked in. Aaron Bedra, principal, Relevance Inc. This book covers the territory of writing great software, and the authors are your experienced guides. If you follow the map that they have drawn, you’ll learn to write only the code that you need, and you’ll write it simply and clearly. Y o u ’ l l come home from this journey with some experiences that will have immediate and lasting ef fects on the code in your editor and the code yet to flow from your fingertips. Craig Demyanovich, 8th Light, Inc. The RSpec Book teaches you much more than how to use RSpec’s fea- tures; it teaches you how to write code the way the RSpec team does: patiently, and with great precision and clarity. There is something here for everyone: beginners are given plenty of gentle attention but there is some real meat for the more experienced reader to chew on, too. Matt Wynne, independent programmer and coach The second generation of tools for the XP generation explained by their creators and maintainers. Awesome, a must read. Marcus Ahvne, software developer, V a l t e c h Some authors would be satisfied with just writing the definitive guide for a technology. These folks go a step further, and show you insider tips that will keep your tests clean and maintainable. Ian Dees, Software Engineer Download from Wow! eBook <www.wowebook.com> www.it-ebooks.info The RSpec Book Behaviour-Driven Development with RSpec, Cucumber, and Friends David Chelimsky with Dave Astels Zach Dennis Aslak Hellesøy Bryan Helmkamp Dan North The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas Download from Wow! eBook <www.wowebook.com> www.it-ebooks.info 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: Jacquelyn Carter Indexing: Potomac Indexing, LLC Copy edit: Kim W i m p s e t t Layout: Steve Peter Production: Janet Furlow Customer support: Ellie Callahan International: Juliet Benda Copyright © 2010 David Chelimsky, Dave Astels, Zach Dennis, Aslak Hellesøy, Bryan Helmkamp, and Dan North. All rights reserved. No part of this publication may be reproduced, stored in a retrieval s ystem, 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-37-9 ISBN-13: 978-1-934356-37-1 Printed on acid-free paper. P1.0 printing, December 2010 V e r s i o n : 2010-11-24 Download from Wow! eBook <www.wowebook.com> www.it-ebooks.info Contents Foreword 11 About the Authors 14 Acknowledgments 16 Preface 18 Ruby and Gem V e r s i o n s . . . . . . . . . . . . . . . . . . . . 18 Downloading the Code Examples . . . . . . . . . . . . . . . 19 What’s in This Book . . . . . . . . . . . . . . . . . . . . . . 19 I Getting Started with RSpec and Cucumber 21 1 Introduction 22 1.1 Test-Driven Development: Where It All Started . . . 22 1.2 Behaviour-Driven Development: The Next Step . . . 24 1.3 RSpec . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.4 Cucumber . . . . . . . . . . . . . . . . . . . . . . . . 26 1.5 The BDD Cycle . . . . . . . . . . . . . . . . . . . . . . 28 2 Hello 30 2.1 Installation . . . . . . . . . . . . . . . . . . . . . . . . 30 2.2 Hello RSpec . . . . . . . . . . . . . . . . . . . . . . . 31 2.3 Hello Cucumber . . . . . . . . . . . . . . . . . . . . . 33 3 Describing Features 37 3.1 Introducing Codebreaker . . . . . . . . . . . . . . . . 38 3.2 Planning the First Release . . . . . . . . . . . . . . . 38 3.3 Planning the First Iteration . . . . . . . . . . . . . . 44 3.4 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 52 Download from Wow! eBook <www.wowebook.com> www.it-ebooks.info CONTENTS 6 4 Automating Features with Cucumber 53 4.1 Steps and Step Definitions . . . . . . . . . . . . . . . 55 4.2 Step Definition Methods . . . . . . . . . . . . . . . . 56 4.3 Test Double . . . . . . . . . . . . . . . . . . . . . . . 60 4.4 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 62 5 Describing Code with RSpec 63 5.1 Getting Started with RSpec . . . . . . . . . . . . . . 63 5.2 Red: Start with a Failing Code Example . . . . . . . 66 5.3 Green: Get the Example to Pass . . . . . . . . . . . . 68 5.4 Refactor . . . . . . . . . . . . . . . . . . . . . . . . . . 72 5.5 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 76 6 Adding New Features 77 6.1 Scenario Outlines in Cucumber . . . . . . . . . . . . 77 6.2 Responding to Change . . . . . . . . . . . . . . . . . 80 6.3 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 83 7 Specifying an Algorithm 84 7.1 Begin with the Simplest Example . . . . . . . . . . . 84 7.2 Refactor to Remove Duplication . . . . . . . . . . . . 88 7.3 Refactor to Express Intent . . . . . . . . . . . . . . . 89 7.4 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 94 8 Refactoring with Confidence 96 8.1 Sniffing Out Code Smells . . . . . . . . . . . . . . . . 96 8.2 One Step at a Time . . . . . . . . . . . . . . . . . . . 98 8.3 Updating Specs After Refactoring . . . . . . . . . . . 107 8.4 Exploratory Testing . . . . . . . . . . . . . . . . . . . 111 8.5 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 112 9 Feeding Back What W e ’ v e Learned 114 9.1 Use Cucumber for Collaboration . . . . . . . . . . . 114 9.2 Experimenting with a New Implementation . . . . . 1 18 9.3 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 122 Report erratum this copy is (P1.0 printing, December 2010) Download from Wow! eBook <www.wowebook.com> www.it-ebooks.info CONTENTS 7 II Behaviour-Driven Development 124 10 The Case for BDD 125 10.1 How TraditionalProjects Fail . . . . . . . . . . . . . 125 10.2 Why TraditionalProjects Fail . . . . . . . . . . . . . 126 10.3 Redefining the Problem . . . . . . . . . . . . . . . . . 130 10.4 T he Cost of Going Agile . . . . . . . . . . . . . . . . . 133 10.5 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 136 11 W r i t i n g Software That Matters 138 11.1 A Description of BDD . . . . . . . . . . . . . . . . . . 138 11.2 T he Principles of BDD . . . . . . . . . . . . . . . . . 138 11.3 T he Project Inception . . . . . . . . . . . . . . . . . . 139 11.4 T he Cycle of Delivery . . . . . . . . . . . . . . . . . . 141 11.5 What’s in a Story? . . . . . . . . . . . . . . . . . . . . 145 11.6 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 148 III RSpec 149 12 Code Examples 150 12.1 Describe It! . . . . . . . . . . . . . . . . . . . . . . . . 151 12.2 P ending Examples . . . . . . . . . . . . . . . . . . . . 155 12.3 Hooks: Before, After, and Around . . . . . . . . . . . 158 12.4 Helper Methods . . . . . . . . . . . . . . . . . . . . . 162 12.5 Shared Examples . . . . . . . . . . . . . . . . . . . . 164 12.6 Nested Example Groups . . . . . . . . . . . . . . . . 166 12.7 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 169 13 RSpec::Expectations 170 13.1 should, should_not, and matchers . . . . . . . . . . 171 13.2 Built-in Matchers . . . . . . . . . . . . . . . . . . . . 172 13.3 P redicate Matchers . . . . . . . . . . . . . . . . . . . 180 13.4 Be Truein the Eyes of Ruby . . . . . . . . . . . . . . 181 13.5 Have Whatever Y o u Like . . . . . . . . . . . . . . . . 182 13.6 Operator Expressions . . . . . . . . . . . . . . . . . . 186 13.7 Generated Descriptions . . . . . . . . . . . . . . . . . 187 13.8 Subjectivity . . . . . . . . . . . . . . . . . . . . . . . . 188 13.9 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 190 Report erratum this copy is (P1.0 printing, December 2010) Download from Wow! eBook <www.wowebook.com> www.it-ebooks.info CONTENTS 8 14 RSpec::Mocks 191 14.1 Test Doubles . . . . . . . . . . . . . . . . . . . . . . . 192 14.2 M ethod Stubs . . . . . . . . . . . . . . . . . . . . . . 193 14.3 M essage Expectations . . . . . . . . . . . . . . . . . 195 14.4 Test-Specific Extensions . . . . . . . . . . . . . . . . 197 14.5 M ore on Method Stubs . . . . . . . . . . . . . . . . . 201 14.6 M ore on Message Expectations . . . . . . . . . . . . 203 14.7 When to Use Test Doubles and Test-Specific Exten- sions . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 14.8 Risks and Trade-Offs . . . . . . . . . . . . . . . . . . 218 14.9 Choosing Other Test Double Frameworks . . . . . . 220 14.10 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 222 15 T o o l s and Integration 224 15.1 T he rspec Command . . . . . . . . . . . . . . . . . . 224 15.2 TextMate . . . . . . . . . . . . . . . . . . . . . . . . . 229 15.3 Autotest . . . . . . . . . . . . . . . . . . . . . . . . . . 230 15.4 Rake . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 15.5 RCov . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 15.6 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 233 16 Extending RSpec 234 16.1 M etadata . . . . . . . . . . . . . . . . . . . . . . . . . 2 34 16.2 Configuration . . . . . . . . . . . . . . . . . . . . . . 235 16.3 F iltering . . . . . . . . . . . . . . . . . . . . . . . . . . 235 16.4 Extension Modules . . . . . . . . . . . . . . . . . . . 238 16.5 Global Hooks . . . . . . . . . . . . . . . . . . . . . . . 239 16.6 M ock Framework . . . . . . . . . . . . . . . . . . . . 239 16.7 Custom Matchers . . . . . . . . . . . . . . . . . . . . 240 16.8 M acros . . . . . . . . . . . . . . . . . . . . . . . . . . 244 16.9 Custom Formatters . . . . . . . . . . . . . . . . . . . 2 47 16.10 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 250 IV Cucumber 251 17 Intro to Cucumber 252 17.1 F rom 20,000 Feet . . . . . . . . . . . . . . . . . . . . 253 17.2 F eatures . . . . . . . . . . . . . . . . . . . . . . . . . 253 17.3 Customer Acceptance Tests . . . . . . . . . . . . . . 257 17.4 Gherkin . . . . . . . . . . . . . . . . . . . . . . . . . . 257 17.5 Scenarios . . . . . . . . . . . . . . . . . . . . . . . . . 259 Report erratum this copy is (P1.0 printing, December 2010) Download from Wow! eBook <www.wowebook.com> www.it-ebooks.info CONTENTS 9 17.6 Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 17.7 T he cucumber Command . . . . . . . . . . . . . . . 261 17.8 Given/When/Then . . . . . . . . . . . . . . . . . . . 262 17.9 Declarative and Imperative Scenario Styles . . . . . 263 17.10 Organizing Features . . . . . . . . . . . . . . . . . . 264 17.11 Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 17.12 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 267 18 Cucumber Detail 269 18.1 Step Definitions . . . . . . . . . . . . . . . . . . . . . 269 18.2 W o r l d . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 18.3 Calling Steps W i t h i n Step Definitions . . . . . . . . 272 18.4 Hooks . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 18.5 Background . . . . . . . . . . . . . . . . . . . . . . . 275 18.6 M ultiline Text . . . . . . . . . . . . . . . . . . . . . . 276 18.7 Tables in Steps . . . . . . . . . . . . . . . . . . . . . 277 18.8 Scenario Outlines . . . . . . . . . . . . . . . . . . . . 278 18.9 Configuration . . . . . . . . . . . . . . . . . . . . . . 280 18.10 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 280 V Behaviour-Driven Rails 282 19 BDD in Rails 283 19.1 Outside-In Rails Development . . . . . . . . . . . . . 284 19.2 Setting Up a Rails 3 Project . . . . . . . . . . . . . . 286 19.3 Setting Up a Rails 2 Project . . . . . . . . . . . . . . 289 19.4 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 289 20 Cucumber with Rails 290 20.1 Step Definition Styles . . . . . . . . . . . . . . . . . . 290 20.2 Direct Model Access . . . . . . . . . . . . . . . . . . . 292 20.3 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 297 21 Simulating the Browser with W e b r a t 298 21.1 W r i t i n g Simulated Browser Step Definitions . . . . . 299 21.2 Navigating to Pages . . . . . . . . . . . . . . . . . . . 304 21.3 M anipulating Forms . . . . . . . . . . . . . . . . . . 308 21.4 Specifying Outcomes with V i e w Matchers . . . . . . 313 21.5 Building on the Basics . . . . . . . . . . . . . . . . . 316 21.6 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 319 Report erratum this copy is (P1.0 printing, December 2010) Download from Wow! eBook <www.wowebook.com> www.it-ebooks.info CONTENTS 10 22 Automating the Browser with W e b r a t and Selenium 320 22.1 Getting Started . . . . . . . . . . . . . . . . . . . . . 321 22.2 W r i t i n g Step Definitions for Selenium . . . . . . . . 324 22.3 Debugging Selenium Issues . . . . . . . . . . . . . . 328 22.4 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 330 23 Rails Views 331 23.1 W r i t i n g V i e w Specs . . . . . . . . . . . . . . . . . . . 331 23.2 M ocking Models . . . . . . . . . . . . . . . . . . . . . 334 23.3 Specifying Helpers . . . . . . . . . . . . . . . . . . . . 341 23.4 When Should I W r i t e V i e w Specs? . . . . . . . . . . . 343 23.5 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 343 24 Rails Controllers 345 24.1 Controller Specs . . . . . . . . . . . . . . . . . . . . . 345 24.2 Context-Specific Examples . . . . . . . . . . . . . . . 352 24.3 Specifying ApplicationController . . . . . . . . . . . 361 24.4 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 363 25 Rails Models 365 25.1 W r i t i n g Model Specs . . . . . . . . . . . . . . . . . . 365 25.2 Specifying Business Rules . . . . . . . . . . . . . . . 373 25.3 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . 382 25.4 U seful Tidbits . . . . . . . . . . . . . . . . . . . . . . 383 25.5 What W e ’ v e Learned . . . . . . . . . . . . . . . . . . . 386 A RubySpec 388 A.1 The Project . . . . . . . . . . . . . . . . . . . . . . . . 389 A.2 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . 389 A.3 Guards . . . . . . . . . . . . . . . . . . . . . . . . . . 391 A.4 Extensibility . . . . . . . . . . . . . . . . . . . . . . . 392 A.5 MSpec . . . . . . . . . . . . . . . . . . . . . . . . . . . 394 A.6 Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394 A.7 Community . . . . . . . . . . . . . . . . . . . . . . . . 395 B RSpec’s Built-in Expectations 397 C Bibliography 401 Index 403 Report erratum this copy is (P1.0 printing, December 2010) Download from Wow! eBook <www.wowebook.com> www.it-ebooks.info [...]... Acceptance Test Driven Development called Acceptance Test Driven Planning (ATDP) In ATDP, we use customer acceptance tests to drive the development of code Ideally, these are the result of a collaborative effort between the customer and the delivery team Sometimes they are written by the delivery team and then reviewed/approved by the customer In either case, they are customer facing and must be expressed... www.it-ebooks.info Preface The RSpec Book is an exploration of Behaviour- Driven Development and tools that Ruby developers use when practicing BDD: RSpec, Cucumber, et al All the tools and libraries used in this book are under regular development with contributions from vibrant communities By the time you read this, there are very likely new releases of many, if not all of them The examples in this book... (red/green/refactor) 1.5 The BDD Cycle At each green point in the RSpec cycle, we’ll check the Cucumber cycle If it is still red, the resulting feedback should guide us to the next action in the RSpec cycle If it is green, we can jump out to Cucumber, refactor if appropriate, and then repeat the cycle by writing a new failing Cucumber step In the next chapter, we’ll get you set up with Cucumber and RSpec and walk... that the value of greeting should equal “Hello RSpec! ” This is the Then of this example: the expected outcome As you’ll see throughout this book, we use these three simple words— Given, When, and Then—because they are easily understood by both technical and nontechnical contributors to a project Now save the file, open a command shell, cd into the directory in which it is saved, and type this command: rspec. .. inside the spec directory If you moved greeter_spec.rb to the spec directory, then you should see output similar to the output you saw at the end of Section 2.2, Hello RSpec, on page 31 There is certainly a lot of detail yet to cover here, but that’s why this is a book and not a blog post! In the chapters that follow, you’ll learn all about RSpec and Cucumber and how to use them in the context of Behaviour- Driven. .. the meme And the meme is subtle And the meme is persistent And the meme will have its way And when it does, our industry will never be the same again Are you willing to risk that? What is this meme? What name shall we give it? The meme is legion! It’s not just Agile, though Agile is there It’s not just TDD and BDD, though both are there It’s not just Continuous Integration, Acceptance Test Driven Development, ... to yourself Just try to stay calm, breathe normally, and keep reading Yes, all the code is in Ruby Yes, all the examples use RSpec and Cucumber to one degree or another Yes, if you read this book, you will learn RSpec, Cucumber, and things about Ruby and Rails and Webrat that you didn’t know before No doubt about it Remember, the best lies are near-truths Here’s the thing While you read this book, you... Getting Started with RSpec and Cucumber Download from Wow! eBook www.it-ebooks.info Chapter 1 Introduction Behaviour- Driven Development began its journey as an attempt to better understand and explain the process of Test -Driven Development Dan North had observed that the developers he was coaching were having a tough time relating to TDD as a design tool and came to the conclusion... do with the word test Dave Astels took that to the next step in the seminal article “A New Look at Test -Driven Development, ”1 in which he suggested that even experienced TDDers were not getting all the benefit from TDD that they could be getting To put this into perspective, perhaps a brief exploration of Test -Driven Development is in order 1.1 Test -Driven Development: Where It All Started Test -Driven. .. say 'Hello RSpec! ' when it receives the greet() message" do greeter = RSpecGreeter.new greeting = greeter.greet greeting.should == "Hello RSpec! " end end Run the file again by typing rspec greeter_spec.rb, and the output should be something like this: Finished in 0.00075 seconds 1 example, 0 failures Success! The dot on the first line represents the one example that was run, and the summary on the last . <www.wowebook.com> www.it-ebooks.info What Readers Are Saying About The RSpec Book: Behaviour- Driven Development with RSpec, Cucumber, and Friends The RSpec Book is a fantastic introduction. <www.wowebook.com> www.it-ebooks.info The RSpec Book Behaviour- Driven Development with RSpec, Cucumber, and Friends David Chelimsky with Dave Astels Zach Dennis Aslak

Ngày đăng: 17/02/2014, 23:20

TỪ KHÓA LIÊN QUAN

w