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

标签 angular input mocking testcase

在我的 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/

相关文章:

c# - 单元测试 EF 的状态管理代码

javascript - 总结输入类型数组并在输出中显示

javascript - 默认值更改时向输入添加类

javascript - JS输入框多吗?

Angular 通用 : lifecycle hook for when window api becomes available

java - PowerMock 模拟外部库

c# - NHibernate 单元测试模拟/内存数据库

angular - 使用 CLI 删除组件的最佳方法是什么

javascript - Angular 2,如何使用服务在组件之间共享数组数据?

javascript - Angular 7 Guard 重定向仅适用于双击