2017-01-05 2 views
10

私は子として動的に作成するコンポーネントのコンポーネントクラスを受け取るコンポーネントを持っています。TestBedに「entryComponents」を提供

let componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentToCreate); 
this.componentReference = this.target.createComponent(componentFactory); 

私はユニットテストを書いて、それがレンダリング&を作成するためのいくつかのTestComponentに合格しようとしています。

TestBed 
    .configureTestingModule(<any>{ 
    declarations: [MyAwesomeDynamicComponentRenderer, TestHostComponent], 
    entryComponents: [TestComponent], 
    }); 

configureTestingModuleentryComponentsを持っているが、私はエラーを取得しないTestModuleMetadata見込んでいるため、「あらゆる」にキャストすることがあります:「TestComponentが見つかりません部品工場を」。

entryComponentsTestBedに提供するにはどうすればよいですか?

答えて

21

さて、私はそれを理解しました。テストでは、モックコンポーネントを宣言し、entryComponentと指定する新しいモジュールを定義する必要があります。

@NgModule({ 
    declarations: [TestComponent], 
    entryComponents: [ 
    TestComponent, 
    ] 
}) 
class TestModule {} 

そしてTestBed

TestBed 
    .configureTestingModule({ 
    declarations: [ValueComponent, TestHostComponent], 
    imports: [TestModule], 
    }); 

にインポート私はそれが誰かを助けることを願っています:]

+0

それは私をたくさん助けてくれました、ありがとう! – trichetriche

+0

はい!できます!!どうもありがとうございます!! – llstarscreamll

+0

StackOverflowだけがあなたに複数回答えをアップアップさせるなら! –

9

また、あなたはしたい場合は、直接あなたのテストファイルにそれを行うことができます。

TestBed.configureTestingModule({ 
    declarations: [ MyDynamicComponent ], 
}).overrideModule(BrowserDynamicTestingModule, { 
    set: { 
    entryComponents: [ MyDynamicComponent ], 
    } 
}); 
+0

これはもっと正しい方法でしょう。 – MBielski

関連する問題