4

我想为使用 pouchdb + angular-pouchdb 的 Angular 应用程序设置单元测试。但是当我运行时:

karma start karma.conf.js

我收到以下错误:

PhantomJS 1.9.7 (Mac OS X) 错误

TypeError:'undefined' 不是函数(评估 'eventEmitter[method].bind(eventEmitter)')

在 .../public/bower_components/pouchdb/dist/pouchdb-nightly.js:5758

角度应用程序在浏览器中运行良好,我所有的 karma.conf.js 文件都包含相同的依赖项,所以我不明白为什么 pouchdb-nightly.js 会有一个未定义的函数。我错过了什么?

4

2 回答 2

9

这是一个常见的错误:PhantomJS 没有实现 Function.prototype.bind (这里是错误),所以你需要es5-shim

或者你可以通过填充 Function.prototype.bind 而不是包含整个 es5-shim 库来解决问题。这是一个示例(取自此处):

(function () {
  'use strict';
  // minimal polyfill for phantomjs; in the future, we should do ES5_SHIM=true like pouchdb
  if (!Function.prototype.bind) {
    Function.prototype.bind = function (oThis) {
      if (typeof this !== "function") {
        // closest thing possible to the ECMAScript 5
        // internal IsCallable function
        throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
      }

      var aArgs = Array.prototype.slice.call(arguments, 1),
          fToBind = this,
          fNOP = function () {},
          fBound = function () {
            return fToBind.apply(this instanceof fNOP && oThis
                   ? this
                   : oThis,
                   aArgs.concat(Array.prototype.slice.call(arguments)));
          };

      fNOP.prototype = this.prototype;
      fBound.prototype = new fNOP();

      return fBound;
    };
  }
})();
于 2014-07-26T16:59:07.647 回答
1

问题已在 PhantomJS 2+ 中修复。所以幻影节点模块的更新应该做到这一点。

使用以下版本的测试相关模块进行测试package.json

"karma": "0.13.21",
"karma-jasmine": "0.3.7",
"karma-phantomjs-launcher": "1.0.0",
"phantomjs-prebuilt": "2.1.3",
于 2016-03-08T16:57:15.733 回答