Prueba angular de 2 unidades: @ViewChild no está definido
Estoy escribiendo una prueba de unidad angular 2. tengo un@ViewChild
subcomponente que necesito reconocer después de que el componente se inicialice. En este caso es unTimepicker
componente de la biblioteca ng2-bootstrap, aunque los detalles no deberían importar. Despues yodetectChanges()
la instancia del subcomponente aún no está definida.
Pseudocódigo:
@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)
}));
});
El pseudocódigo funciona como se espera hasta llegar al registro de la consola. lostimepickerChild
es indefinido. ¿Por qué está pasando esto?