How to replace a component used in @viewChildren for a test double?
You can use reflect-metadata features to do it working:
it('should have four sons', () => {
const propMetadata = Reflect['getMetadata']('propMetadata', FatherComponent);
var originType = propMetadata.cpxs[0].selector;
propMetadata.cpxs[0].selector = ComplexComponentStub; // Replace ViewChild Type
let fixture = TestBed.createComponent(FatherComponent);
let comp = fixture.componentInstance as FatherComponent;
fixture.detectChanges();
expect(comp.cpxs.length).toBe(4);
propMetadata.cpxs[0].selector = originType; // reset ViewChild
});
Test in Plunker
You can read more about decorators and about reflect-metadata here:
- Angular 2, decorators and class inheritance