2017-10-05 5 views
2

私はいくつかのTypescriptクラスをJasmineでテストするために取り組んでいます。私はジャスミンが一緒に同じ道をテストする構成だジャスミンテストではどういう意味ですか?Uncaught TypeError:ObjectプロトタイプはObjectまたはnullのみです:未定義?

module xxx.DeviceData { 
    export class ConsoleListener extends DataListener { 
     constructor() { 
      super("ConsoleListener"); 
     } 

     addData(data: string) { 
      console.log(this.title + ": " + data); 
     } 

     clear() { 
      console.clear(); 
     } 
    } 
} 

module xxx.DeviceData { 
describe('ConsoleListener test', function() { 
    var consoleListener, 
     consoleListenerObj; 

    beforeEach(() => { 
     consoleListener = jasmine.createSpy("consoleListener"); 

     consoleListenerObj = jasmine.createSpyObj('consoleListenerObj', ['onSuccess', 'onFailure', 'addData', 'clear']); 
     consoleListenerObj.onSuccess(); 
     consoleListenerObj.onFailure(); 
     consoleListenerObj.addData(); 
     consoleListenerObj.clear(); 
    }); 

    it('test#1 columnDefinition defined', function() { 
     let consoleListener = new ConsoleListener(); 

     expect(consoleListener).not.toBeNull(); 
    }); 

    it('test#2 call onSuccess', function() { 
     expect(consoleListenerObj.onSuccess).toHaveBeenCalled(); 
    }); 

    it('test#3 call onFailure', function() { 
     expect(consoleListenerObj.onFailure).toHaveBeenCalled(); 
    }); 

    it('test#4 call addData', function() { 
     expect(consoleListenerObj.addData('123')); 
    }); 

    it('test#5 call clear', function() { 
     expect(consoleListenerObj.clear()); 
    }); 
}); 
} 

このすべてtranspilesを完璧に私は、モジュールの宣言を介して相互に構成するいくつかのクラスを作成しました。私は、テストを実行しようとすると、私は

キャッチされない例外TypeErrorこのエラーが表示されます。スクリプト/ DeviceData/ConsoleListener.js時には不定:Objectプロトタイプは、オブジェクトまたはnullの場合も5:27

何かが間違って起こっています蒸留されたJSの5行目にありますか?ここにある:

var __extends = (this && this.__extends) || (function() { 
    var extendStatics = Object.setPrototypeOf || 
     ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || 
     function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; 
    return function (d, b) { 
     extendStatics(d, b); 
     function __() { this.constructor = d; } 
     d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 
    }; 
})(); 
var xxx; 
(function (xxx) { 
    var DeviceData; 
    (function (DeviceData) { 
     var ConsoleListener = (function (_super) { 
      __extends(ConsoleListener, _super); 
      function ConsoleListener() { 
       return _super.call(this, "ConsoleListener") || this; 
      } 
      ConsoleListener.prototype.addData = function (data) { 
       console.log(this.title + ": " + data); 
      }; 
      ConsoleListener.prototype.clear = function() { 
       console.clear(); 
      }; 
      return ConsoleListener; 
     }(DeviceData.DataListener)); 
     DeviceData.ConsoleListener = ConsoleListener; 
    })(DeviceData = xxx.DeviceData || (xxx.DeviceData = {})); 
})(xxx|| (xxx= {})); 
//# sourceMappingURL=ConsoleListener.js.map 

確かに、5行目はオブジェクトとプロトタイプについて話しているようです。

私は、モジュールを互いに話し合わせるためのさまざまな方法を試しましたが、このモジュールアプローチは、私が一貫して動作することができる唯一の方法です。ここに通らなければならないカルマ/ジャスミンの文脈に欠けているものがありますか?ここで

は私karma.configです:

module.exports = function (config) { 
     config.set({ 

      frameworks: ["jasmine","karma-typescript"], 

      preprocessors: { 
       "Scripts/**/*.ts": ["karma-typescript"] 
      }, 

      files: [ 
       'Scripts/DeviceData/*.ts', 
       'Scripts/UnitTests/*.spec.ts' 
      ], 

      exclude: [ 
       'Scripts/**/NodeJSDataSocket.ts' 
      ], 
      reporters: ["progress", "karma-typescript"], 

      //reporters: ["dots", "karma-typescript"], 

      browsers: ["Chrome"], 
      karmaTypescriptConfig: { 
       compilerOptions: { 
        "module": "commonjs", 
        "sourceMap": true, 
        "target": "es5" 
"moduleResolution": "classic", 
"noImplicitAny": false 
       }, 
       tsconfig: "./tsconfig.json", 
      }, 
     }); 
    }; 

答えて

2

それはあなたがスーパークラスから継承したときに活字体が注入extends機能から来ているようなエラーが見えます。それはおそらく完全に欠落していない

extends DataListener 

、そうでない場合、コンパイラは警告を表示します - それはどちらかである:あなたがそれを使用する場合、コードを見てみると

、私はあなたのDataListenerはあなたに利用できないでしょうねJasmineが実行されているときにインクルードされていない(ロードされていない)、またはあなたが物事がうまく読み込まれていない。

ご注文のうえ、うまくいけば...喜び!

 files: [ 
      'Scripts/DeviceData/DataListener.ts', 
      'Scripts/DeviceData/ConsoleListener.ts', 
      'Scripts/UnitTests/*.spec.ts' 
     ], 
+0

優秀 - これは振り返って明らかです。私の最初の考えは、それがkarma.configに含まれていなかったということでしたが、それはすべきです - 私は上記を投稿しました。それが含まれない理由についての他のアイデア?その間、私は単体テストファイルを持っています。私はこれを確認するために分離してテストすることができます。 –

+0

それはうまくいったので、間違いなくそこに継承されています。 –

+0

どのファイルが 'DataListener'ですか? – Fenton

1

__extends() functionは、クラス継承を処理するためにTypeScriptコンパイラによって生成されます。 bは基底クラスを意味し、dは派生クラスを意味します。したがって、基本クラスDataListenerが欠落しているという問題があります。スクリプトのコンパイルとバンドル方法を確認してください。 1つの名前空間(moduleのキーワードを参照)は複数のファイルにまたがって定義できますが、それらの収集/バンドルは手作業で行うか、compiler option --outFileで処理する必要があります。

関連する問題