使用pouchdb设置karma单元测试



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

karma start karma.conf.js

我得到以下错误:

PhantomJS 1.9.7 (Mac OS X) ERROR

TypeError: 'undefined'不是一个函数(求值'eventEmitter[method].bind(eventEmitter)')

…/公共/bower_components/pouchdb/dist/pouchdb-nightly.js: 5758

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

这是一个常见的错误:PhantomJS没有实现Function.prototype.bind(这就是bug),所以你需要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;
    };
  }
})();

在PhantomJS 2+中修复了这个问题。因此,更新Phantom节点模块应该可以做到这一点。

package.json

中使用以下版本的测试相关模块进行了测试
"karma": "0.13.21",
"karma-jasmine": "0.3.7",
"karma-phantomjs-launcher": "1.0.0",
"phantomjs-prebuilt": "2.1.3",

最新更新