Book Image

Backbone.js Testing

By : Ryan Glenn Roemer
Book Image

Backbone.js Testing

By: Ryan Glenn Roemer

Overview of this book

Table of Contents (13 chapters)
Backbone.js Testing
About the Author
About the Reviewers


JavaScript web applications are soaring in popularity and driving exciting new application possibilities across the Internet. One of the most ubiquitous frameworks leading this charge is Backbone.js, which provides a modern and rational approach for organizing JavaScript applications.

At the same time, testing client-side JavaScript and Backbone.js applications remains a difficult and tedious undertaking. Even experienced developers can stumble across issues related to browser idiosyncrasies, complex DOM interactions, and asynchronous application behavior when writing frontend tests.

Backbone.js Testing brings sensible practices and current techniques to the challenges of Backbone.js test development. You will be introduced to fundamental testing concepts, a contemporary frontend test infrastructure, and practical exercises on all facets of Backbone.js application development. This book covers topics ranging from basic test suite creation to using test doubles to tackle even the most difficult/least testable Backbone.js application components.

With a little guidance from this book, you can test your Backbone.js web applications easily, quickly, and with confidence.

What this book covers

Chapter 1, Setting Up a Test Infrastructure, starts with the basics of how to set up your test application code and obtain the test libraries that we will use throughout this book. We create a basic test infrastructure, write the first tests, and review the test report results.

Chapter 2, Creating a Backbone.js Application Test Plan, begins with a refresher of Backbone.js fundamentals, introduces a sample web application for the book, and discusses a wide range of relevant testing and planning concepts. We conclude by writing and running our first Backbone.js application tests.

Chapter 3, Test Assertions, Specs, and Suites, covers the basics of writing Backbone.js test suites and specs with Mocha and test assertions with Chai.

Chapter 4, Test Spies, introduces the Sinon.JS test double library and how to spy on application method behaviors in Backbone.js tests.

Chapter 5, Test Stubs and Mocks, dives deeper into Sinon.JS, with stubs and mocks that can replace application method behaviors. We examine how stubs and mocks can reduce application dependencies in tests and facilitate easier and more tractable Backbone.js application tests.

Chapter 6, Automated Web Testing, enhances the test infrastructure built in the previous chapters to run automatically, for example, from the command line or a continuous integration server.

Who this book is for

This book is for JavaScript developers who are looking to create and implement test support for Backbone.js web applications. You should be comfortable with the JavaScript programming language and familiar with Backbone.js application development including the core components such as models, views, and routers, although you may be just learning the framework as you explore the testing topics of this book. Some exposure to testing methodology and technologies (in any language) would be helpful but not required.


In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We simulate slow tests using the native JavaScript function setTimeout()."

A block of code is set as follows:

describe("Test failures", function () {
  it("should fail on assertion", function () {

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

describe("Test failures", function () {
  it("should fail on assertion", function () {

Any command line input or output is written as follows:

$ mocha-phantomjs chapters/05/test/test.html

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "clicking the Next button moves you to the next screen".


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

The source code for all the examples and files in this book are available at the GitHub repository ( and introduced in more detail at The website will always contain the most current and updated instructions for obtaining and using the code examples for this book.


The code samples repository internally uses symbolic links for some libraries and files. Accordingly, Windows users may need to download the samples archive from Packt (see the ensuing instructions) instead of GitHub.

As this is an open source project, the examples may be periodically updated to fix bugs or to clarify code or concepts. Thus, the code snippets in the book may not exactly match the online code samples, but there should not be too much difference in practice. Ultimately, you can rely on the GitHub repository as the most correct version of the code in this book.

Due to limitations in the Chai assertion library, the minimum browser requirements for running the examples are as follows:

  • Chrome: 7+

  • Safari: 5+

  • Firefox: 4+

  • Internet Explorer: 9+

The vendor library versions that we use in this book include the following:

  • Backbone.js: 1.0.0

  • Underscore.js: 1.4.4

  • jQuery: 2.0.2

  • Mocha: 1.9.0

  • Chai: 1.7.1

  • Sinon.JS: 1.7.3

The GitHub repository will attempt to keep up with the changes as these libraries continue to evolve over time. At the same time, most of the application and testing samples in the book should continue to work well with the updated libraries for the foreseeable future, except where specifically noted otherwise in this book or on the website.

Files and code for each chapter are provided via a directory structure of chapters/NUMBER, where NUMBER is the chapter number. The example Backbone.js web application—Notes—is available in a localStorage version in the notes directory and as a full MongoDB-backed Node.js server in notes-rest.

To retrieve the example code, you can download the entire zipped archive from: Another option is to use git to checkout the source code directly:

$ git clone

Finally, you can download the example code files for all Packt books you have purchased from your account at If you have purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from


Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.


You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.