I'm currently writing unit tests for my backbone.js application, and I'm having a few problems testing the backbone routes with QUnit.
Unfortuntely, I'm getting the following error when attempting to run the test. It appears to be on the QUnit module setup when I create an instance of my Backbone Router:
Cannot call method 'start' of undefined
Here is my code for the test's module setup (called on document ready):
var router;
module("Test Router", {
setup: function () {
router = new TestRouter();
},
teardown: function () {
router = null;
}
});
And here is code for my router:
var TestRouter = Backbone.Router.extend({
initialize: function () {
Backbone.history.start();
},
routes: {
"!/test-view": "testView",
"!/test-view-two": "testViewTwo",
"*actions": "testView"
},
testView: function () {
//view code here
},
testViewTwo: function () {
//view code here
}
});
Does anyone have any ideas of what I'm doing wrong? The code works as expected outside of the QUnit test.
Url Hash modifications changes are tracked when you call Backbone.history.start()
and it was not designed to be used inside the constructor of router. Please change your code to below and give it a try
var router;
module("Test Router", {
setup: function () {
router = new TestRouter();
Backbone.history.start();
},
teardown: function () {
router = null;
}
});
var TestRouter = Backbone.Router.extend({
initialize: function () {
},
routes: {
"!/test-view": "testView",
"!/test-view-two": "testViewTwo",
"*actions": "testView"
},
testView: function () {
//view code here
},
testViewTwo: function () {
//view code here
}
});