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 で

angular アプリはブラウザーで適切に動作し、すべての karma.conf.js ファイルには同じ依存関係が含まれているため、pouchdb-nightly.js に未定義の関数がある理由がわかりません。私は何が欠けていますか?

4

2 に答える 2

9

これは一般的なエラーです。PhantomJS は Function.prototype.bind を実装していないため (ここにバグがあります)、es5-shimが必要です。

または、es5-shim ライブラリ全体を含める代わりに、Function.prototype.bind をシミングするだけで済みます。例を次に示します (ここから取得):

(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+ で修正されています。そのため、Phantom ノード モジュールを更新する必要があります。

以下のバージョンのテスト関連モジュールでテスト済み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 に答える