2016-04-28 31 views
1

所以我使用Gulp,Gulp-Jasmine和SystemJS來測試一個Angular2演示程序。真的很簡單。我已經得到了一個System.config塊工作,規範文件加載,但它試圖運行具有以下錯誤beforeEach塊時倒下......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] }

我一飲而盡任務概述如下...

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)); 
}); 

我的規格文件看起來像這樣...

/// <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並且它不存在。導入茉莉花將無濟於事,因爲它在node.js中加載時不會改變全局對象。

how do I get past this error ???

那麼,我會建議有幾種方法。如果您打算在Web瀏覽器中運行此代碼,我會建議在瀏覽器中而不是在節點中測試代碼。

  1. 請按照(不完整)指南來測試angular2 docs。這涉及到建立一個你將用來運行測試的html文件。如果您計劃使用gulp
  2. 使用this angular2 seed(或另一個)作爲關於如何一起使用Angular2,SystemJs,Jasmine和Karma的模型,那麼這不是您想要的。業力隊員默認爲Chrome,但如果您願意,您可以將其更改爲PhantomJS。這很有效,上週我剛剛爲angular1應用做了這個。

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

已經有自打字稿1.5一個更好的辦法,我全心全意地推薦它。將tsconfig.json file添加到項目的根目錄。當然,這已經在我上面鏈接的angular2種子中爲你完成了。