OReilly.JavaScript.Testing.With.Jasmine.Apr.2013.ISBN.1449356370.pdf

(1449 KB) Pobierz
JavaScript Testing with Jasmine
Evan Hahn
JavaScript Testing with Jasmine
by Evan Hahn
Copyright © 2013 Evan Hahn. 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.
Editor:
Mary Treseler
Production Editor:
Marisa LaFleur
Proofreader:
Rachel Monaghan
March 2013:
First Edition
Cover Designer:
Karen Montgomery
Interior Designer:
David Futato
Illustrator:
Rebecca Demarest
Revision History for the First Edition:
2013-03-22:
First release
See
http://oreilly.com/catalog/errata.csp?isbn=9781449356378
for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc. _JavaScript Testing with Jasmine_, the image of a phoebe, 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 author assume no
responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
ISBN: 978-1-449-35637-8
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v
1. Intro to Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What Is Software Testing?
Why Is It Useful?
Test-Driven Development
Behavior-Driven Development
1
2
2
2
5
5
6
6
7
8
9
2. Jasmine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
What Is Jasmine?
Getting Set Up with Jasmine
Testing Existing Code with describe, it, and expect
An Example to Test
Jasmine Time!
Matchers
Writing the Tests First with Test-Driven Development
Cardinal Rule: When in Doubt, Test
Test Components
Black-Box Testing
3. Writing Good Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
13
13
14
15
15
16
17
17
18
4. Matchers in Depth. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Equality: toEqual
Identity: toBe
Yes or No? toBeTruthy, toBeFalsy
Negate Other Matchers with not
Check If an Element Is Present with toContain
Is It Defined? toBeDefined, toBeUndefined
iii
V413HAV
Nullness: toBeNull
Is It NaN? toBeNaN
Comparators: toBeGreaterThan, toBeLessThan
Nearness: toBeCloseTo
Using toMatch with Regular Expressions
Checking If a Function Throws an Error with toThrow
Custom Matchers
Before and After
Nested Suites
Skipping Specs and Suites
Matching Class Names
18
18
19
19
20
20
20
23
24
24
25
27
29
30
30
30
31
5. More Jasmine Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6. Spies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
The Basics: Spying on a Function
Calling Through: Making Your Spy Even Smarter
Making Sure a Spy Returns a Specific Value
Replacing a Function with a Completely Different Spy
Creating a New Spy Function
Creating a New Spy Object
Jasmine and CoffeeScript
Jasmine and Node.js
Installing jasmine-node on Unix and Linux
Installing jasmine-node on Windows
Basic Usage
Asynchronous Tests with jasmine-node
jasmine-node and CoffeeScript
Jasmine and Ruby on Rails
Installation
Usage
Jasmine with Non-Rails Ruby
More Tools
Jasmine on the Web
The Basic Structure of a Suite
Matchers Reference
List of Falsy Values
Reserved Words in Jasmine
7. Using Jasmine with Other Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
33
34
34
34
34
35
35
36
36
36
37
37
39
39
40
40
40
8. Reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
iv
|
Table of Contents
Preface
All programmers want their code to work the way they intended. Jasmine, a popular
testing framework for the JavaScript programming language, allows you to achieve that
goal. Through coded specifications, Jasmine helps make your JavaScript work exactly
how it’s supposed to. In this book, we’ll explore Jasmine in detail, from its basic concepts
to its advanced features.
This book aims to explain the concepts of testing and test-driven development, as well
as why they’re useful. It then aims to dive into Jasmine and explain how it can help
programmers test their JavaScript code. By the end of this book, I aim to give readers
an understanding of Jasmine’s concepts and syntax.
Who Should Read This Book
This book is intended for programmers who are familiar with some more advanced
JavaScript features, such as closures and callbacks, and who have a general understand‐
ing of JavaScript’s prototype system. If you are interested in learning how to write reliable
JavaScript code, this is the book for you.
Jasmine is useful when building a maintainable and scalable JavaScript application, ei‐
ther in a browser or on a server. It can help ensure that a browser’s client-side data
models are performing properly, or that a server is correctly serving pages.
Jasmine is also useful for building reliable JavaScript libraries. It can help ensure that
the exposed API of your library matches what you intend it to match.
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, email addresses, filenames, and file extensions.
v
Zgłoś jeśli naruszono regulamin