Testing in Backbone
Uses Mocha
Runs both in Node and the browser. Has no assertion library built-in. Uses Chai for assertions. (install chai with bower)
Sinon.js for spies and stubs
Also mocking for ajax and timers - will show later
He has an index.html file in the /test directory. The current Github repo uses qUnit and is much different.Configuring require.js: Different config for running and testing. The goal is that they don’t diverge too much. Shows config.js:
require.config({
deps: ['test/main'],
baseUrl: '../app',
paths: {
jquery: '../components/jquery/jquery',
underscore: '../components/lodash/lodash',
backbone: '../components/backbone-amd/backbone',
markdown: '../components/markdown/lib/markdown',
moment: '../components/moment/moment',
tpl: '../components/requirejs-tpl/tpl',
'backbone.localStorage': '../components/backbone.localStorage/backbone.localStorage',
test: "../test"
},
shim: {
markdown: {
exports: 'markdown'
}
}
});
Tests are loaded async with requirejs. Uses the traditional AMD module definitions:
define([
'test/app-spec'
], function(){
if(window.mochaPhantomJS){
window.mochaPhantomJS.run()
} else {
window.mocha.run();
}
})
This has to run after mocha has loaded.