我试图在一个Angular 应用程序中测试主组件和子组件之间的交互.我不知道如何在创建父组件时获取对子组件的引用.以下是设置:
child.component.spec.ts个
@Component({template: `<child [data]="model"></child>`})
class HostComponent {
public model:any;
}
describe('ChildComponent', () => {
let hostFixture: ComponentFixture<HostComponent>;
let childFixture: ComponentFixture<ChildComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ChildComponent, HostComponent]
});
}));
beforeEach(() => {
// this creates the child component as well, but how to get to it?
hostFixture = TestBed.createComponent(HostComponent);
// this creates a NEW instance of child component, not the one from the Host template
// so it's not the instance I actually want to test
childFixture = TestBed.createComponent(ChildComponent);
});
});
改变hostFixture.componentInstance
中的model
值实际上不会改变childFixture.componentInstance
的data
输入值;这就是我如何意识到有两个子组件的实例.
我的问题很简单,我怎样才能得到childFixture
来引用HostComponent
模板中的组件fixture ,而不是我目前拥有的另一个实例?
The docs美元没用.