简体   繁体   English

以角度运行单元测试

[英]Running unit test in angular

I am running unit test for angular app.I expect ng init to be called 我正在为角度app运行单元测试。我希望调用init

Component.ts: Component.ts:

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

service.ts service.ts

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

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

Actual test: 实际测试:

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

expected: test passed ; 预期:测试通过; Actual:TypeError: Cannot read property 'createSpyObj' of undefined 实际:TypeError:无法读取未定义的属性“createSpyObj”

the error: Actual:TypeError: Cannot read property 'createSpyObj' of undefined says that mockService is undefined. 错误: Actual:TypeError: Cannot read property 'createSpyObj' of undefined表示mockService未定义。 So the test is not completed. 所以测试没有完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM