如何为无头backbone.router测试配置jsdom

时间:2015-03-24 13:15:18

标签: backbone.js mocha sinon jsdom

我想要获得一个无头的backbone.router测试来使用jsdom但是在解决这个神秘的错误时遇到了一些问题:

  Route
    MinRouter
      1) "before each" hook

  0 passing (167ms)
  1 failing

  1) Route "before each" hook:
     Uncaught TypeError: Cannot call method 'toLowerCase' of undefined
      at _.extend.start (/home/user/min/vendor/backbone.js:1398:68)
      at /home/user/min/__test__/uroute/testrouter.js:14:30
      at Object.jsdom.env.done (/home/user/min/__test__/helpers/clientenv.js:30:13)
      at /home/user/min/node_modules/jsdom/lib/jsdom.js:249:18
      at process._tickCallback (node.js:419:13)

以下是如何设置jsdom env:

jsdom.env({
    html: html,
    done: function(errs, window) {
        global.window = window;
        ...
        global.BP.Router = {};
        global.navigator = {  browser: 'foo'   };
        callback();
    }
});

1 个答案:

答案 0 :(得分:1)

证明了这在jsdom

中是必需的
jsdom.env({
    html: html,
    done: function(errs, window) {
        global.window = window;
        ...
        global.BP.Router = {};
        global.navigator = {  userAgent: 'node-js' };
        callback();
    }
});