我目前正在为我的 backbone.js 应用程序编写单元测试,我在使用 QUnit 测试主干路由时遇到了一些问题。
不幸的是,我在尝试运行测试时收到以下错误。当我创建我的主干路由器的实例时,它似乎在 QUnit 模块设置上:
无法调用未定义的方法“start”
这是我的测试模块设置代码(在文档准备好时调用):
var router;
module("Test Router", {
setup: function () {
router = new TestRouter();
},
teardown: function () {
router = null;
}
});
这是我的路由器的代码:
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
}
});
有人知道我做错了什么吗?该代码在 QUnit 测试之外按预期工作。
最佳答案
当您调用 Backbone.history.start()
时,会跟踪 Url Hash 修改更改,并且它并非设计用于在路由器的构造函数中使用。请将您的代码更改为以下并试一试
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
}
});
关于javascript - 无法使用 Backbone.history.start() 和 QUnit 调用未定义错误的方法 'start',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9416984/