簡體   English   中英

angular2 / testing beforeEach不是一個函數

[英]angular2/testing beforeEach is not a function

所以我使用Gulp,Gulp-Jasmine和SystemJS來測試Angular2演示應用程序。 真的很簡單。 我設法讓一個System.config塊工作,並加載spec文件,但是當嘗試運行帶有以下錯誤的beforeEach塊時,它會失敗...

{[錯誤:TypeError:_global.beforeEach不是Object.eval上的函數(D:/ ng2demo / node_modules / angular2 / src / testing / matcher s.js:26:9)在eval(D:/ ng2demo / node_modules / angular2 / src / testing / matchers.js:20 5:4)在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)在eval(D:/ng2demo/node_modules/angular2/src/testing/testing.js:245:4)處於eval(D: / e2demo/node_modules/angular2/src/testing/testing.js:246:3)在eval(native)評估D:/ng2demo/node_modules/angular2/src/testing/matchers.js評估D:/ ng2demo / node_modules / angular2 /src/testing/testing.js評估D:/ng2demo/node_modules/angular2/testing.js加載D時出錯:/ng2demo/app/assets/services/config.service.spec.js] originalErr:[TypeError:_global.beforeEach不是一個功能]}

我的gulp任務概述如下......

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);
    });
});
  • 我做錯了什么,如何通過這個錯誤???
  • 我需要在規范頂部的參考路徑還是有更好的方法?

TypeError:_global.beforeEach不是Object.eval的函數

錯誤來自angular2/testing ,您可以查看源代碼以了解原因。 它試圖從全局對象導入beforeEach ,但它不存在。 導入jasmine無濟於事,因為在node.js中加載時不會改變全局對象。

我如何通過這個錯誤???

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

  1. 按照angular2文檔中的(不完整)指南進行測試。 這涉及設置一個用於運行測試的html文件。 如果您計划使用gulp自動化測試運行,那么這不是您想要的
  2. 使用此angular2種子 (或其他)作為模型,了解如何一起使用Angular2,SystemJs,Jasmine和Karma。 業力轉輪默認為Chrome ,但如果您願意,可以將其更改為PhantomJS 這很好用,我上周剛剛為angular1應用做了這個。

我需要在規范頂部的參考路徑還是有更好的方法?

從Typescript 1.5開始,有一種更好的方法,我全心全意地推薦它。 tsconfig.json文件添加到項目的根目錄。 當然,這已經在我上面鏈接的angular2種子中為你完成了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM