2016-04-28 12 views
1

私はGulp、Gulp-Jasmine、SystemJSを使ってAngular2デモアプリケーションをテストしています。本当に簡単です。私は私の一気のタスクは以下の通りですangular2/testing beforeEachは関数ではありません

{ [Error: TypeError: _global.beforeEach is not a function at Object.eval (D:/ng2demo/node_modules/angular2/src/testing/matcher s.js:26:9) at eval (D:/ng2demo/node_modules/angular2/src/testing/matchers.js:20 5:4) at eval (D:/ng2demo/node_modules/angular2/src/testing/matchers.js:20 6:3) at eval (native) at Object.eval (D:/ng2demo/node_modules/angular2/src/testing/testing .js:10:18) at eval (D:/ng2demo/node_modules/angular2/src/testing/testing.js:245 :4) at eval (D:/ng2demo/node_modules/angular2/src/testing/testing.js:246 :3) at eval (native) Evaluating D:/ng2demo/node_modules/angular2/src/testing/matchers.js Evaluating D:/ng2demo/node_modules/angular2/src/testing/testing.js Evaluating D:/ng2demo/node_modules/angular2/testing.js Error loading D:/ng2demo/app/assets/services/config.service.spec.js] originalErr: [TypeError: _global.beforeEach is not a function] }

...次のエラーでbeforeEachブロックを実行しようとすると、しかし、それは倒れる、ロードするために動作するようにSystem.configブロックを取得するために管理し、specファイルしています...

var gulp = require('gulp'); 
var jasmine = require('gulp-jasmine'); 
var System = require('systemjs'); 

gulp.task('newtest',() => { 
    System.config({ 
     baseURL: "", 
     transpiler: 'typescript', 
     defaultJSExtensions: true, 
     packages: { 
      app: { 
       format: 'register', 
       defaultExtension: 'js' 
      }, 
     },  
     map:{   
      'angular2':'node_modules/angular2', 
      'rxjs':'node_modules/rxjs', 
     }, 
    }); 

    Promise.all([ 
     System.import('app/assets/services/config.service.spec'), 
    ]).then(function(){  
     gulp.src(['app/assets/services/config.service.spec']) 
      .pipe(jasmine()) 
    }).catch(console.error.bind(console)); 
}); 

私のspecファイルは次のようになります...

/// <reference path="../../../typings/browser/ambient/systemjs/index.d.ts"/> 
/// <reference path="../../../typings/browser/ambient/jasmine/index.d.ts"/> 

import {Injector, provide} from "angular2/core"; 
import {Http, BaseRequestOptions, Response, ResponseOptions} from "angular2/http"; 
import {MockBackend} from "angular2/http/testing"; 
import {ConfigService} from "./config.service"; 
import {it, describe, beforeEach, inject, expect} from "angular2/testing"; 

describe("ConfigService",() => { 

    // THIS IS THE LINE THAT FAILS !!! 
    beforeEach(() => { 
     //Do some prep 
    }); 
    it("it does a test and evals to true",() => { 
     expect(true).toBe(true); 
    }); 
}); 
  • 私が間違って何をやっている、どのように私はこのエラーを乗り越えますか?
  • 仕様の先頭にリファレンスパスが必要ですか、それとも良い方法がありますか?
+0

これは実際のコードではないと思われます。あなたはあなたの中かっことカッコをコメントアウトしました//準備中のコメントを投稿します – Austin

+0

上記のコメントを編集しましたが、それでもうまくいきません!!! – HeavenlyHost

答えて

0

TypeError: _global.beforeEach is not a function at Object.eval

エラーがangular2/testingから来ている、あなたはなぜ見ることがlook at the sourceことができます。グローバルオブジェクトからbeforeEachをインポートしようとしていますが、存在しません。 jasmineをインポートすることは役に立ちません。なぜなら、node.jsにロードするときにグローバルオブジェクトを変更しないからです。

how do I get past this error ???

さて、いくつかの方法があります。このコードをWebブラウザで実行する予定の場合は、ノードではなくブラウザでコードをテストすることをお勧めします。

  1. angular2 docsのテストの(不完全な)ガイドに従ってください。これには、テストの実行に使用するhtmlファイルの設定が含まれます。
  2. this angular2 seed(または別のもの)をモデルとして、Angular2、SystemJs、Jasmine、Karmaを一緒に使用する方法をモデルとして使用してください。カルマランナーのデフォルトはChromeですが、好きな場合はPhantomJSに変更することができます。これはかなりうまくいく、私はちょうどAng1アプリのためにこれを先週行った。

Do I need the reference paths at the top of the spec or is there a better way ?

は活字体1.5以降のより良い方法があった、と私は心からそれをお勧めします。プロジェクトのルートにtsconfig.json fileを追加します。これはもちろん、私が上にリンクしているangular2シードであなたのために既に行われています。

関連する問題