Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Backbone.js Testing
Backbone.js Testing

Backbone.js Testing:

eBook
£7.99 £25.99
Paperback
£32.99
Subscription
Free Trial
Renews at £16.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Backbone.js Testing

Chapter 2. Creating a Backbone.js Application Test Plan

Now that we have a basic test infrastructure, we'll turn our attention toward integrating a Backbone.js application and mapping out a test development strategy. In this chapter, we will create a test plan by working through the following topics:

  • Reviewing some fundamental concepts of Backbone.js development

  • Selecting a Backbone.js application to test

  • Examining relevant testing concepts and approaches to guide test plan creation and execution

  • Evaluating parts of the Backbone.js application to test in complete or partial isolation

  • Identifying tests that exercise multiple interacting parts of the Backbone.js application

  • Integrating the Backbone.js application into our test infrastructure, and authoring and running a few introductory application tests

A Backbone.js refresher


Although this book assumes a comfortable level of familiarity with Backbone.js, Underscore.js, and jQuery, we will briefly touch upon the basics of Backbone.js application development.

Backbone.js provides abstractions and useful functionality for architecting and developing JavaScript web applications. Backbone.js brings order to the chaotic interactions between program and display logic, DOM events, and backend communication. This is achieved via what could loosely be considered a Model-View-Controller (MVC) paradigm that separates application code into the following topics:

  • Data modeling and retrieval

  • Display rendering and user interactivity

  • Brokering data and display logic to appropriately bind and manipulate data models and user interfaces

Note

Backbone.js does not completely follow a traditional MVC approach, causing some observers to call it an MV* framework. An MV* application has a model and a view but has something other than a controller connecting the model...

Selecting a Backbone.js application to test


Devising and implementing a test plan is fundamentally a practical exercise, and we can better achieve our goal of overall application reliability by applying test lessons and techniques to a real application—whether it is one that has just been started or an existing application in need of better test coverage.

If you already have a Backbone.js application in development, you can most likely skip to the next section of this chapter. One potential issue we want to identify is the complexity of the existing application, especially one with minimal or no existing tests. Complicated dependencies, non-modular design, and highly coupled application components may require extensive mocking and stubbing to even permit a base level of test framework integration. Ultimately, the test infrastructure written around a legacy application will likely be quite different from the one written around a modular, decoupled application such as Notes. Accordingly, you...

Test paradigms and methods


There are numerous competing and complementary theories regarding software testing and development methodologies. Reading up on the world of test methods provides an excellent background for any developer looking to improve the ways in which they construct, implement, and manage tests. For brevity's sake, we will only introduce two paradigms in this book that are particularly useful for Backbone.js testing—Test-Driven Development (TDD) and Behavior-Driven Development (BDD).

Test-Driven Development is a process wherein tests are written first and then the actual code is written. The benefits of this approach include:

  • Making tests a first-class priority in the development process

  • Encouraging code to be written in small modular units

  • Preventing the knowledge of code implementation details from unduly influencing the tests

TDD and general software testing principles are covered in many resources; a recommended reference on the subject is Growing Object-Oriented Software...

Testing concepts, approaches, and planning


Before jumping into the testing waters, it makes sense to have a plan of what we should test and why. The term test plan is heavily overloaded with many potential interpretations as processes, literature, and practices have been prevalent and continuously evolving for decades. It is not surprising that modern test plans can range from casual, mostly ad-hoc practices to formal, 100-page documents requiring executive sign offs at various stages.

Note

For a more detailed discussion on the test plan practices applied to modern JavaScript applications, see the JavaScript Testing Beginner's Guide by Yuxian Eugene Liang (http://www.packtpub.com/javascript-testing-beginners-guide/book).

As Backbone.js applications are typically created in iterative development cycles, often without a lot of extra formality, we will take a fairly practical approach and create a test plan that simply identifies testing categories and applies them to the application under test...

Testing individual Backbone.js components


Backbone.js applications are quite amenable to testing separation. Backbone.js provides a small number of core components that mostly avoid interdependencies. Our goal in this section is to identify the different parts of a Backbone.js application that can be unit tested in isolation and start thinking about the features of each one that we should test. Many components can simply be instantiated alone while others will need some extra mocking or patching help in our tests.

Models

Backbone.js models most often are independent entities that can be instantiated with a simple new MyModel({foo: 123}) invocation. Accordingly, we can create standalone model objects in our tests without references to any other objects. Our model tests should include the assertions that:

  • Objects can be instantiated with supplied and/or default values

  • Data can be synchronized with a backing datastore (for example, localStorage or a REST server)

  • Custom and built-in events fire and...

Testing application interactions and events


A Backbone.js application is used as a cohesive whole by end users, and wherever possible, we should have the test infrastructure verify overall application functionality and behaviors that cut across single Backbone.js components.

Partial integrations

While unit tests are a staple of modern software development, we must metaphorically move from unit-testing trees to the forest of partial integration tests to ensure that at least some pieces of the application work together harmoniously and reliably. In practice, this just means varying the degree to which we mock or remove component dependencies in the tests we discussed previously.

Integration tests can interact with application parts in many ways, including:

  • By creating a parent view with a collection and subviews, invoking DOM events, and checking appropriate changes are made to both the collection data and subview displays

  • By filling in and submitting the form input in a Backbone.js view

  • By directly...

Dipping our toes in the application testing waters


Now that we can identify the aspects of the Backbone.js components that we want to test, let's begin planning and writing tests for the namespace utility and the Backbone.js model. For each component, we will examine application use cases and expected behaviors and then write tests to verify our expectations.

Namespace

The starting point for the Notes application is a namespace utility that provides two global variables to organize our application classes (App) and instance (app). In the notes/app/js/app/namespace.js example application file, we'll create the two namespace object literals with class/application properties:

// Class names.
var App = App   || {};
App.Config      || (App.Config = {});
App.Models      || (App.Models = {});
App.Collections || (App.Collections = {});
App.Routers     || (App.Routers = {});
App.Views       || (App.Views = {});
App.Templates   || (App.Templates = {});

// Application instance.
var app = app || {};

The...

Running the application tests


With our Backbone.js application files and the preliminary application tests ready, we need to integrate everything into the test driver we created in Chapter 1, Setting Up a Test Infrastructure. We will continue with the existing application directory structure by adding specs to chapters/02/test/js/spec and copying the application libraries from notes/app/js/app to chapters/02/app/js/app.

Note

The Notes application resides in the notes/app directory, which is the base location we will use to discuss the application components. At the same time, the chapter code examples are aimed to be independent. Therefore, we maintain our layout rules that the application code goes in chapters/NUMBER/app and the tests go in chapters/NUMBER/test.

Accordingly, the downloadable examples link files such as chapters/02/app/js/app/namespace.js to notes/app/js/app/namespace.js. Thus, throughout this book, we will talk about a file such as namespace.js interchangeably using either...

Summary


In this chapter, we reviewed the basics of Backbone.js applications and introduced the Notes application as a helpful, if optional, companion to the test examples in this book. We then reviewed some relevant high-level testing concepts and dived into the specifics of what we will want to test in a Backbone.js application—in separated application pieces and as integrated parts of a whole. Finally, we wrote our first application unit tests and extended the test infrastructure from Chapter 1, Setting Up a Test Infrastructure, to execute our test reporter.

You should now be able to take an existing or in-development Backbone.js application, analyze its components, and create an abstract test infrastructure outline that will later be filled in with actual tests and suites.

In the next chapter, we will learn about Chai assertions, basic Mocha test constructs (specs and suites), test setup/teardown/configuration, and how to deterministically verify behavior in an asynchronous application...

Left arrow icon Right arrow icon

What you will learn

  • Install and set up a test infrastructure for Backbone applications
  • Run, examine, and understand Mocha test reports
  • Tour the Chai assertion library API with detailed examples
  • Write test suites and specs with the Mocha test framework
  • Fake application behavior in tests using Sinon.JS mocks, spies, and fake servers

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 12, 2013
Length: 168 pages
Edition :
Language : English
ISBN-13 : 9781782165255
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jul 12, 2013
Length: 168 pages
Edition :
Language : English
ISBN-13 : 9781782165255
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
£16.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
£169.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just £5 each
Feature tick icon Exclusive print discounts
£234.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just £5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total £ 32.99
Backbone.js Testing
£32.99
Total £ 32.99 Stars icon
Banner background image

Table of Contents

6 Chapters
Setting Up a Test Infrastructure Chevron down icon Chevron up icon
Creating a Backbone.js Application Test Plan Chevron down icon Chevron up icon
Test Assertions, Specs, and Suites Chevron down icon Chevron up icon
Test Spies Chevron down icon Chevron up icon
Test Stubs and Mocks Chevron down icon Chevron up icon
Automated Web Testing Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.9
(14 Ratings)
5 star 92.9%
4 star 7.1%
3 star 0%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Jacob Kronika Oct 09, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Over the past few weeks, I have spent time reading Backbone.js Testing at the request of the author, Ryan Roemer. This goes through the motions of creating a Notes application (think of a very stripped down and rudimentary Evernote) from the ground up, demonstrating thoroughly the process of testing the implementation throughout.The GoodEvery aspect of this text is well planned and executed. From an overview of the Backbone.js architecture to the Notes application itself, I was very pleased with the content and arrangement. Some of the topics covered by the author include:* Backbone.js Application Development* Mocha Testing Framework* Chai Assertion Framework* Sinon.JS Test Double Library* Test Automation with Selenium and PhantomJSNot So GoodThere really was little or nothing that I can criticize about Backbone.js Testing. The language used was clear and simple, while conveying even the most complex of topics in a way that, I believe, any level of JavaScript programmer would be able to digest easily. There were a few points at which the content, being fairly dense in terms of code snippets to delve through, was somewhat lacking in luster. However, even those areas were well-concieved, with the relevent code highlighted and some areas snipped. The full content remains available for download in the accompanying code samples, and even includes extensive implementation and testing details not specifically covered in the book itself.Overall ImpressionsFar and away, Backbone.js Testing is, in itself, a solid backbone for JS testing in general, while containing specifics related only to the name from which it's title is derived. In addition, the book provides an overarching view of the subject matter without either treading on any given area within too lightly or delving too deep. Finally, there is more than sufficient information in the book, and more so in the code samples, to give anyone looking to develop a test architecture a firm foundation for their implementation.
Amazon Verified review Amazon
Nizzy Sep 24, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I thoroughly enjoyed this book. Sure, there are tutorials online that cover the same topics but I prefer reading from a physical book. The chapters are short and clear but give a lot of insight into Backbone testing.I had never written a single test in Javascript prior to reading this book and I feel quite confident in my ability to do so now. For that alone, I have no regrets purchasing this book.The only drawbacks I could think of were the brief explanations of asynchronous behavior and Sinon fake servers. A couple more paragraphs and examples would have been nice but the author does point you the literature of the product libraries. Those two features deserve more than what was provided because in my experience they are frequently used.
Amazon Verified review Amazon
George V. Reilly Oct 27, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Backbone.js Testing is a short, dense introduction to testing JavaScript applications with three testing libraries, Mocha, Chai, and Sinon.JS. Although the author uses a sample application of a personal note manager written with Backbone.js throughout the book, much of the material would apply to any JavaScript client or server framework.Mocha is a test framework that can be executed in the browser or by Node.js, which runs your tests. Chai is a framework-agnostic TDD/BDD assertion library. Sinon.JS provides standalone test spies, stubs and mocks for JavaScript. They complement each other and the author does a good job of explaining when and how to use each.I've written a lot of tests in Python (unittest and mock, primarily) and C# (NUnit), but my experience with JavaScript unit testing was both limited and years out of date. The JavaScript ecosystem continues to evolve rapidly, with new browser frameworks and Node packages springing up everywhere. JavaScript has some particular challenges in testing--notably, the DOM, asynchrony, and callbacks. Mocha, Chai, and Sinon meet those challenges, though they can't take away all the pain.The author describes how to test Backbone models, views, and collections; dealing with asynchrony; provides useful testing heuristics, including isolating components to reduce dependencies; when to use stubs and mocks and fake servers; and test automation with PhantomJS. He does not, however, teach you Backbone.js itself; for that, you'll need another book.There are a few areas which I thought were dealt with too lightly. There's no real discussion of Test-driven_development or Behavior-driven_development, which provide the intellectual foundations of much of the book. Nor does he have much to say about testability and how to make legacy code more testable. The sample Notes app has plenty of testing seams (much of this falls naturally out of the architecture of Backbone); other people's apps are not so lucky. The chapter on automation is extremely terse--it could be expanded into a very large book!--but it does provide useful indicators to many areas for exploration.I learned a lot from this book and I have no hesitation in recommending it.Disclosure: Thanks to Ryan Roemer and Packt for a review copy of this book.
Amazon Verified review Amazon
Mansur Suleman Nov 05, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I love this book as it actually provides insight on testing all JS applications. Definitely a great choice to web professionals both in front end and back end Javascript technologies.
Amazon Verified review Amazon
threepwood Jul 27, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Any single tip in this book can save you hours of desperate research. Just the explanation on how to make sure events are bound for testing worths the few bucks I spent.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.