While we continue to explore the theory and practice of testing Backbone.js applications, it remains true that creating test architectures and writing good test specifications are more of an art than an exact science. Many lessons can only be learned through experience, particularly as your applications encounter bugs and development mishaps. In the meantime, we can start you off with some techniques and suggestions.
Application development is a journey guaranteed to include inexplicable errors, sudden application crashes, and complex test failures. When these pitfalls happen, it is important to have some directions on how to debug the problems and move things forward.
A common scenario during software development is when application changes break one or more existing unit tests. In this case, a good practice is to run the test suite one test at a time, fix the test, and then move on to the rest. Mocha provides two avenues to help in this regard:
test.html?grep=PATTERN
.only
helper, skipping all other tests and suites. Let's look at an example:it("doesn't run this test", function () {
expect(true).to.be.true;
});
it.only("runs this test", function () {
expect(false).to.be.false;
});
On the other side of this scenario, sometimes we want to ignore a few failing tests while still using the rest of the test infrastructure. In this case, we look towards skip
.
skip
modifier omits a single specification from the test run and can be applied to multiple specifications. Skipped tests are also considered pending and can be visually distinguished in a Mocha HTML test report:it.skip("doesn't run this test", function () {
expect(true).to.be.true;
});
it("runs this test", function () {
expect(false).to.be.false;
});
Beyond the practical aspects of writing tests, an equally important component of developing a test infrastructure is writing testable application code. The topic of testable JavaScript code is quite large—we will only introduce the issue here and start you off with a general goal of developing application code that works in harmony with the tests that support it.
A comprehensive treatment of the subject is available in the book Testable JavaScript by Mark Ethan Trostler (http://shop.oreilly.com/product/0636920024699.do), which covers topics such as application code complexity, event-based architectures, and debugging. Also consider general JavaScript application guides such as Maintainable JavaScript by Nicholas Zakas (http://shop.oreilly.com/product/0636920025245.do) and the seminal JavaScript: The Good Parts by Douglas Crockford (http://shop.oreilly.com/product/9780596517748.do).
Some Backbone.js application development hints and good practices for testable code include:
model: Foo
) or a model object can be passed to a view on instantiation (for example, new View({model: foo})
). The latter technique often opens up more opportunities for injecting mocks or test-friendly models into the view code. The same logic also applies to the el
property in Backbone.js views—it is often more test-friendly to provide values via a view object instance than in the view class definition.localStorage
datastore. The previous examples that override notes/app/js/app/config.js
provide a good introduction on how to both create a configuration file and supersede values for testing purposes.Please note that these tips are heuristics and not hard and fast rules. Many development situations will favor doing exactly the opposite of one or more of these suggestions. Hopefully, some of these guidelines will help make your early application decisions easier to live with as your application and test code bases grow over time.
35.170.81.33