gpt4 book ai didi

angular - 如何在 Angular 分量测试中模拟 @INPUT 值

转载 作者:行者123 更新时间:2023-12-04 02:59:06 26 4
gpt4 key购买 nike

在我的 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;
});

最佳答案

因为 subComponentinputObj 是公共(public)的,你可以简单地覆盖它,如果你测试 MainComponent 你可以有类似的东西:

component.subComponent = {inputObj: {id: 'someId', name :'someName'}};

关于angular - 如何在 Angular 分量测试中模拟 @INPUT 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50957291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com