Chapter 10. Keeping the Bugs Out – How to Test a Backbone Application

In this chapter, we'll look at the challenges of testing a Backbone application, including the following:

  • Choosing a unit testing framework
  • Deciding between the BDD and the TDD testing styles
  • Mocking out related components in tests
  • Using Selenium to create acceptance-level tests

Testing in JavaScript?

For many years, the idea of testing JavaScript code was laughable. After all, who would write a testing suite for a bunch of form validation scripts? Moreover, even if you were doing something more interesting that could actually benefit from testing, there were no JavaScript libraries that facilitated testing, so it had to be done from scratch.

In 2008, all this changed as a wave of new automated JavaScript testing tools emerged. All of these libraries focused on unit testing or testing of a specific part of the code (usually, a single method or small class). Two of the first unit testing libraries, QUnit and Jasmine, offered dueling approaches (TDD versus BDD) for testing in the browser, while a third library, JS Test Driver, offered testing at the command line. Soon after them came a number of other unit testing libraries, including Buster.js and Mocha.js, as well as test support or mock libraries such as JsMockito, JSMock, and Sinon.JS.

Today, as JavaScript developers create ever more advanced web applications (particularly, the kind made possible by Backbone), testing has become absolutely essential. Testing can prevent bugs that, because of the code's complexity, would otherwise have been difficult or even impossible to find. Further, perhaps, just as importantly, a proper suite of tests allows developers to safely refactor their code, which is a frequent and necessary task for keeping a code base maintainable.

At this point, it's no exaggeration to say that if you are a building a serious web application using Backbone, a testing suite isn't just a luxury, it's a necessity.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.21.47