私は多数のサブコンポーネントを持つテスト対象のコンポーネントを持っています。私のテストでは、これらのサブコンポーネントのいくつかのオブジェクトへの参照を取得して、プロパティが正しく動作しているかどうかを確認し、対話的に変更するようにしたいと考えています。 (例:セグメントボタンコンポーネントがどの状態になっているかを確認するか手動で別の状態にする)テスト:サブコンポーネントのコンポーネントインスタンスを検索する方法
私は角度に関するテストドキュメントを見てきましたが、これを行う方法はありません。私はおそらく、私は、サブコンポーネントのデバッグ要素を見つけてそれをcomponentInstance
にアクセスするために、フィクスチャのdebugElement
のクエリを実行することができますが、それは常にnull
のようです。ドキュメントを見て例えば
:https://angular.io/docs/ts/latest/guide/testing.html#!#component-inside-test-host
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ DashboardHeroComponent, TestHostComponent ], // declare both
}).compileComponents();
}));
beforeEach(() => {
// create TestHostComponent instead of DashboardHeroComponent
fixture = TestBed.createComponent(TestHostComponent);
testHost = fixture.componentInstance;
heroEl = fixture.debugElement.query(By.css('.hero')); // find hero
fixture.detectChanges(); // trigger initial data binding
});
私はTestHostComponent
内DashboardHeroComponent
への参照を取得する方法をしたいと思います。誰もこれを行う方法を知っていますか?
'@ HostBinding'を使用して' .hero'クラスをコンポーネントに追加しますか?あるいは '@ Component'' selector'で使用しますか? – RomanHotsiy
ドキュメントの完全な例は、次のとおりです。https://angular.io/resources/live-examples/testing/ts/app-specs.plnkr.htmlコンポーネントセレクタを使用しているようです。 – Allen