2016-08-15 99 views
3

使用Angular2.0.0-rc.5我試圖運行下面的試驗,但在接收到錯誤發現「DynamicTestModule」沒有NgModule元數據:在單元測試

Error: No NgModule metadata found for 'DynamicTestModule'. (line 29)

DynamicTestModule似乎是角本身的一部分,而不是一個模塊我在我的項目具有一定的參考,也可以在文檔頁面找到angular.io

import {inject, async, TestBed} from '@angular/core/testing'; 
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} from '@angular/platform-browser-dynamic/testing'; 

describe('AuthService',() => { 

    beforeEach(() => { 
     TestBed.initTestEnvironment(
      BrowserDynamicTestingModule, 
      platformBrowserDynamicTesting() 
     ).configureTestingModule({ 
      imports: [ 
       {ngModule: CommonModule, providers: [ 
        ... // overrides for testing 
       ]} 
      ]}); 
    }); 

    it('should get and set a token saved in storage', inject([AuthService], (instance: AuthService) => { 
     instance.token = 'test-token'; 
     expect(instance.token).toEqual('test-token'); 
    })); 
}); 

搜索谷歌的錯誤信息返回任何結果,他們的changelog不會在本例中提到這一點。

有沒有人看到這個具體的錯誤,並知道發生了什麼,或者你可以提供一個使用內置的DI測試服務的例子2.0.0-rc.5沒有這個結果的錯誤?

+0

[沒有跡象表明模塊](https://angular.io/docs/ts/latest/api/core/index/NgModuleMetadata-class.html#!#imports-anchor)可以是一個普通的對象。像'{imports:[CommonModule],providers:[...]}'應該可以工作,不是嗎? – estus

+0

@estus'imports'語句需要一個'Array '。我正在傳遞['ModuleWithProviders'](https://angular.io/docs/ts/latest/api/core/index/ModuleWithProviders-interface.html),因此語法是有效的。 – SnareChops

回答

2

如果使用SystemJS模塊加載程序加載角度模塊,則應該使用單個文件,而不是捆綁包。

在索引文件加載SystemJS

<script src="node_modules/systemjs/dist/system.src.js"></script> 

之前從quickstart application

<script src="systemjs.config.js"></script> 

裝載systemjs.config.js你需要設置

<script>System.packageWithIndex = true;</script> 
+0

在我的情況下,systemjs.config.js沒有檢查packageWithIndex標誌,但是從「捆綁」切換到「單個文件」(已被註釋掉)解決了問題。 – Dave

+0

等待,你爲什麼「需要」設置它?我沒有看到它在使用了systemjs的地方使用,例如https://angular.io/docs/ts/latest/quickstart.html –

+0

最新的Angular版本是2.0.0。答案與2.0.0-rc.5 –

0

我也有類似的後問題:

SCRIPT5022: No NgModule metadata found for 'AppModule'. 

對我來說,this angular2 issue comment修復了各種IE版本(特別是IE10)的問題。