[英]Check property of child component in parent component's spec
I have a parent component and a child component.我有一个父组件和一个子组件。 From the parent's spec file I'd like to test weather a property in the child component has been set.
我想从父级的规范文件中测试是否设置了子组件中的属性。
In the ngOnInit()
of ChildComponent
I can console.log the value of myFormGroup.disabled
and see the value I expect, but I'd like to confirm this in the spec of the parent.在 ChildComponent 的
ChildComponent
ngOnInit()
中,我可以 console.log myFormGroup.disabled
的值并查看我期望的值,但我想在父级的规范中确认这一点。
describe('ParentComponent', () => {
let component: ParentComponent;
let fixture: ComponentFixture<ParentComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ParentComponent, ChildComponent],
imports: [IonicModule.forRoot()],
}).compileComponents();
fixture = TestBed.createComponent(ParentComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));
it('should disable the myFormGroup in child component', () => {
expect(ChildComponent.prototype.myFormGroup.disabled).toBeTrue();
// Cannot read property 'disabled' of undefined
});
});
You can use the By.directive
selector to select the child element.您可以使用
By.directive
选择器来选择 select 子元素。
Try this (follow comments with:!):试试这个(跟随评论:!):
describe('ParentComponent', () => {
let component: ParentComponent;
let fixture: ComponentFixture<ParentComponent>;
// !! have a handle on ChildComponent
let childComponent: ChildComponent;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ParentComponent, ChildComponent],
imports: [IonicModule.forRoot()],
}).compileComponents();
fixture = TestBed.createComponent(ParentComponent);
component = fixture.componentInstance;
fixture.detectChanges();
// !! get a handle on childComponent
childComponent = fixture.debugElement.query(By.directive(ChildComponent)).componentInstance;
}));
it('should disable the myFormGroup in child component', () => {
// !! change assertion
expect(childComponent.myFormGroup.disabled).toBeTrue();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.