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

Testable JavaScript ppt

273 580 0

Đ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 273
Dung lượng 18,71 MB

Nội dung

www.it-ebooks.info www.it-ebooks.info Mark Ethan Trostler Testable JavaScript www.it-ebooks.info ISBN: 978-1-449-32339-4 [LSI] Testable JavaScript by Mark Ethan Trostler Copyright © 2013 ZZO Associates. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com. Editors: Simon St. Laurent and Meghan Blanchette Production Editor: Christopher Hearse Copyeditor: Audrey Doyle Proofreader: Rachel Head Indexer: Lucie Haskins Cover Designer: Randy Comer Interior Designer: David Futato Illustrator: Rebecca Demarest January 2013: First Edition Revision History for the First Edition: 2013-01-14 First release See http://oreilly.com/catalog/errata.csp?isbn=9781449323394 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Testable JavaScript, the image of a Doctor fish, and related trade dress are trademarks of O’Reilly Media, Inc. 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 O’Reilly Media, Inc., was aware of a trade‐ mark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. www.it-ebooks.info For Inslee, Walter, and Michelle—Trostlers Trostlers Trostlers Woo! www.it-ebooks.info www.it-ebooks.info Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix 1. Testable JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Prior Art 2 Agile Development 3 Test-Driven Development 5 Behavior-Driven Development 5 The Best Approach? 6 Code Is for People 6 Why 7 What 9 How 9 Beyond Application Code 11 Testing 11 Debugging 11 Recap 12 2. Complexity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Code Size 14 JSLint 20 Cyclomatic Complexity 24 Reuse 27 Fan-Out 29 Fan-In 38 Coupling 39 Content Coupling 39 Common Coupling 40 Control Coupling 40 Stamp Coupling 40 Data Coupling 40 v www.it-ebooks.info No Coupling 41 Instantiation 41 Coupling Metrics 41 Coupling in the Real World 42 Testing Coupled Code 44 Dependency Injection 45 Comments 48 YUIDoc 48 JSDoc 50 Docco/Rocco 52 The Human Test 54 Recap 55 3. Event-Based Architectures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 The Benefits of Event-Based Programming 57 The Event Hub 58 Using the Event Hub 61 Responses to Thrown Events 65 Event-Based Architectures and MVC Approaches 67 Event-Based Architectures and Object-Oriented Programming 68 Event-Based Architectures and Software as a Service 68 Web-Based Applications 68 Testing Event-Based Architectures 69 Caveats to Event-Based Architectures 73 Scalability 73 Broadcasting 73 Runtime Checking 73 Security 74 State 74 A Smarter Hub: The Event Switch 74 Deployment 75 An Implementation 77 Sessions 79 Extensibility 80 Recap 80 4. Unit Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 A Framework 83 Let’s Get Clean 84 Writing Good Tests 86 Isolation 87 Scope 87 vi | Table of Contents www.it-ebooks.info Defining Your Functions 88 Positive Testing 89 Negative Testing 90 Code Coverage 90 Real-World Testing 91 Dependencies 91 Asynchronous Testing 94 Running Tests: Client-Side JavaScript 95 PhantomJS 95 Selenium 100 Running Tests: Server-Side JavaScript 107 Jasmine 107 Recap 113 5. Code Coverage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Coverage Basics 115 Code Coverage Data 118 A Hands-on Example 119 Instrumenting Files 119 Anatomy of a Coveraged File 120 Exercise/Deploy 121 Client-Side JavaScript 121 Server-Side JavaScript 123 Persisting Coverage Information 126 Unit Tests 126 Integration Tests 127 Generating Output 128 Aggregation 129 Hidden Files 131 Coverage Goals 134 Recap 135 6. Integration, Performance, and Load Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Integration Testing 137 Selenium 138 CasperJS 144 Performance Testing 146 Generating HAR Files 147 Viewing HAR Files 152 Browser Performance Testing 154 Load Testing 156 Browser Load Testing 157 Table of Contents | vii www.it-ebooks.info Tracking Resource Usage 162 Client-Side Tracking 163 Server-Side Tracking 169 Recap 172 7. Debugging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 In-Browser Debugging 173 Firefox 175 Chrome 178 Safari 181 Internet Explorer 183 Node.js Debugging 183 Remote Debugging 186 Chrome 187 PhantomJS 192 Firefox 193 Mobile Debugging 194 Android 4 194 iOS 6 195 Adobe Edge Inspect 197 Other Mobile Debugging Options 201 Production Debugging 201 Minified Code 202 Source Maps 203 Recap 208 8. Automation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 What to Automate 209 When to Automate 209 How to Automate 210 Automating with Continuous Integration 210 Automating the Development Environment 211 Automating the Build Environment 221 Deployment 240 Recap 241 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 viii | Table of Contents www.it-ebooks.info [...]... leads to testable code, let alone testable JavaScript Similarly, TDD, BDD, and other forms of development also do not necessarily lead to testable JavaScript What does lead to testable JavaScript? A com‐ mitment to writing clear, loosely coupled, and well-commented code that you know will be maintained by someone else will lead to testable JavaScript Writing, reading, and maintaining testable JavaScript. .. the “what” and the “how” of testable JavaScript Chapter 1, Testable JavaScript The overrriding theme of this book is writing and maintaining testable code But what is testable code? Why should we strive to write it? And how do we do so? We will begin by exploring all of these questions, and taking a look at some popular development methodologies and how they relate to testable code Ultimately, what‐... algorithm” to the mundane “a JavaScript newbie wrote it and it’s nasty,” and everything in between Analyzing the complexity of static code is a great starting point toward creating testable JavaScript Maintainable JavaScript is clear, consistent, and standards-based Testable JavaScript is loosely coupled, short, and isolatable The magic happens when your code is both maintainable and testable Since you will... from easier testing to simpler debugging, testable JavaScript is your gateway to sanity Most importantly, do not forget that you are writing code for people, not for the compiler People, yourself included, will have to maintain your code going forward, so make ev‐ eryone’s life easier, including your own, and write testable JavaScript 12 | Chapter 1: Testable JavaScript www.it-ebooks.info CHAPTER 2 Complexity... www.it-ebooks.info | 11 Recap Testable JavaScript does not automatically flow from Agile, waterfall, TDD, BDD, or whatever other software development philosophy you might follow Testable JavaScript is a commitment to small, loosely coupled, bite-sized chunks of simple code How you arrive at that code is up to you This book will hopefully provide insight into achieving this state of coded bliss Writing testable code... to increase With the advent of JavaScript, we must apply the lessons learned not only from other languages, but also from JavaScript itself to make our code testable As JavaScript ap‐ plications grow in size, on both the client and the server, we must be extremely careful to apply the best practices and lessons learned by our forefathers and tweak them to fit well with JavaScript 1 www.it-ebooks.info... Writing testable code will make your life, and the lives of all who follow you, much, much easier From fewer bugs to more easily fixed ones, from easier testing to simpler debugging, testable JavaScript is your gateway to sanity This book attempts to show you the path toward that sanity After reading the entire book you will have a very good understanding of what writing and maintaining testable JavaScript. .. virtuous circles again I like virtuous circles How testable Writing testable code is easier when you start with a blank slate (isn’t everything easier when you start with a blank slate?) Some development methodologies, such as TDD and BDD, can lead to testable code, but not necessarily This is because having tests for code does not automatically make that code testable However, when you think about testing... rather than harder? How about running tests on both client- and server-side JavaScript? These topics and more (debugging, anyone?) are all covered within this book, so get ready for a wild ride through the world of JavaScript development The overriding theme is writing and maintaining testable code Content This book will tackle testable code in several steps First we will investigate complexity Then... “how” to write testable code and “how” to test it But before we can get to the “how,” it is important to understand the “what” and the “why” of software development Why do we want to write testable code? What is testable code? Why “Writing software is the hardest thing people do,” says Douglas Crockford, which means it is extremely important that software be as human-friendly as possible Testable code . “how” of testable JavaScript. Chapter 1, Testable JavaScript The overrriding theme of this book is writing and maintaining testable code. But what is testable. www.it-ebooks.info www.it-ebooks.info Mark Ethan Trostler Testable JavaScript www.it-ebooks.info ISBN: 978-1-449-32339-4 [LSI] Testable JavaScript by Mark Ethan Trostler Copyright

Ngày đăng: 17/03/2014, 12:20

TỪ KHÓA LIÊN QUAN

w