私は角度2ユニットテストを書いています。私はコンポーネントが初期化された後に認識する必要がある@ViewChild
サブコンポーネントを持っています。この場合、ng2-bootstrapライブラリのTimepicker
コンポーネントですが、詳細は重要ではありません。 I detectChanges()
の後、サブコンポーネントのインスタンスは未定義です。角度2ユニットテスト - @ViewChildは定義されていません
擬似コード:予想通り
@Component({
template: `
<form>
<timepicker
#timepickerChild
[(ngModel)]="myDate">
</timepicker>
</form>
`
})
export class ExampleComponent implements OnInit {
@ViewChild('timepickerChild') timepickerChild: TimepickerComponent;
public myDate = new Date();
}
// Spec
describe('Example Test',() => {
let exampleComponent: ExampleComponent;
let fixture: ComponentFixture<ExampleComponent>;
beforeEach(() => {
TestBed.configureTestingModel({
// ... whatever needs to be configured
});
fixture = TestBed.createComponent(ExampleComponent);
});
it('should recognize a timepicker'. async(() => {
fixture.detectChanges();
const timepickerChild: Timepicker = fixture.componentInstance.timepickerChild;
console.log('timepickerChild', timepickerChild)
}));
});
擬似コードを使用すると、コンソールログに到達するまで動作します。 timepickerChild
は未定義です。なぜこうなった?
回答を見つけましたか?私は同じ問題を抱えています。 – user3495469