作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 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/
我是一名优秀的程序员,十分优秀!