Angular 2.0.0 - 測試“由模塊導入'DynamicTestModule'”

[英]Angular 2.0.0 - Testing “ imported by the module 'DynamicTestModule' ”


I am having a problem in testing app.component.ts in Angular 2. I am using angular-cli. Whenever I run ng test, my app.component.spec.ts makes the console prompt with the error:

我在測試Angular 2中的app.component.ts時遇到問題。我正在使用angular-cli。每當我運行測試時,我的app.component.spec.ts會使控制台提示錯誤:

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

I imported the HomeModuleComponent in TestBed

我在TestBed中導入了HomeModuleComponent

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

Can anyone help me with this problem?

任何人都可以幫我解決這個問題嗎?

2 个解决方案

#1


20  

HomeModuleComponent is Component not the Module, so it has to be in declarations:

HomeModuleComponent是Component而不是Module,因此它必須在聲明中:

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

and then you can create the component to test as,

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

TestBed.createComponent(AppComponent);

#2


2  

In my test spec, by mistake I was importing a service instead of providing it. I was getting the same error.

在我的測試規范中,我錯誤地導入了一個服務而不是提供它。我得到了同樣的錯誤。

Adding the service back inside providers array resolved my error.

在services數組中添加服務解決了我的錯誤。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/10/13/720d5e28e5c7791c4f1eaab08c0cf998.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com