在我的 Angular 应用程序中,我正在为具有 @Input 值的组件编写测试用例。 我怎样才能模拟 @Input 值。 主组件的 testSave() 方法使用子组件的 InputObject 的 id。 当我运行测试用例时,它说 undefined is an object at 'this.subComponent.InputObject.id;'
export class SubComponent implements OnInit {
@Input() inputObj: InputObject;
}
export class MainComponent implements OnInit {
@Input() subComponent: SubComponent;
testsave() {
this.subComponent.InputObject.id;
}
}
export class InputObject {
contructor(id:string, name: string)
}
测试用例:
it('should save', fakeAsync(() => {
// const event: MockEvent = new MockEvent();
// fixture = TestBed.createComponent(MainComponent);
// component = fixture.componentInstance;
});
最佳答案
因为 subComponent
和 inputObj
是公共(public)的,你可以简单地覆盖它,如果你测试 MainComponent
你可以有类似的东西:
component.subComponent = {inputObj: {id: 'someId', name :'someName'}};
关于angular - 如何在 Angular 分量测试中模拟 @INPUT 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50957291/