2016-10-13 177 views
16

我在測試app.component.ts在Angular 2中有問題。我正在使用angular-cli。每當我跑NG測試,我app.component.spec.ts使得控制檯提示符出現錯誤:角度2.0.0 - 測試「模塊導入'DynamicTestModule'」

Failed: Unexpected directive 'HomeModuleComponent' imported by the module 'DynamicTestModule' 
Error: Unexpected directive 'HomeModuleComponent' imported by the module 'DynamicTestModule' 

我進口HomeModuleComponent在測試牀

TestBed.configureTestingModule({ 
    declarations: [AppComponent], 
    imports : [ HomeModuleComponent ] 
}); 

誰能幫我解決這個問題?

+3

我想我已經找到了問題截至目前。 HomeModuleComponent必須位於不在導入中的聲明中。而且,您可以在TestBed中導入模塊嗎?謝謝。 – xiotee

回答

17

HomeModuleComponentComponent不是Module,所以它必須是在聲明:

TestBed.configureTestingModule({ 
    declarations: [AppComponent, HomeModuleComponent], 
    imports : [ ] 
}); 

,然後你可以創建測試的組件,

TestBed.createComponent(AppComponent);