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
R$49.99 R$196.99
Paperback
R$245.99
Subscription
Free Trial
Renews at R$50p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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

Shipping Address

Billing Address

Shipping Methods
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
Estimated delivery fee Deliver to Brazil

Standard delivery 10 - 13 business days

R$63.95

Premium delivery 3 - 6 business days

R$203.95
(Includes tracking information)

Product Details

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

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Brazil

Standard delivery 10 - 13 business days

R$63.95

Premium delivery 3 - 6 business days

R$203.95
(Includes tracking information)

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
R$50 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
R$500 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 R$25 each
Feature tick icon Exclusive print discounts
R$800 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 R$25 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total R$ 245.99
Backbone.js Testing
R$245.99
Total R$ 245.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

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela