角度运行单元测试

时间:2019-04-01 15:49:26

标签: angular unit-testing

我正在为角度应用程序运行单元测试。我希望ng init被调用

Component.ts:

ngOnInit() {
    this.topToolBarService.change.subscribe(customer => {
      this.filter.customerid = customer;
      this.customerid = customer;
      this.getData();
    });

service.ts

@Output() change: EventEmitter<boolean> = new EventEmitter();

sendCustomer(customer) {
    this.customer = customer;
    this.change.emit(customer);
}

实际测试:

it('should call ngonint',()=>{
    mockService.createSpyObj();
    mockService.change = of("0LPT_id0306");
    fixture.detectChanges();
    expect(fixture.componentInstance.ngOnInit).toHaveBeenCalled();
  })
  

预期:测试通过; Actual:TypeError:无法读取未定义的属性'createSpyObj'

1 个答案:

答案 0 :(得分:0)

错误: Actual:TypeError: Cannot read property 'createSpyObj' of undefined说,模拟服务未定义。因此测试尚未完成。