如何在UnitTest Angular4中调用/检查方法内的值

时间:2017-11-29 11:15:01

标签: angular unit-testing karma-runner karma-mocha

我的.ts文件中有2个方法

 export class VehicleSaleInfoComponent implements OnInit, OnDestroy {
dealerBlock = true;
constructor(){}
ngOnInit() {}

runList() {
    this.dealerBlock = false;
 }

Dealer() {
    this.dealerBlock = true;
}

}

我的.spec文件

  it('should Show RunList Component exist', () => {
    expect(component.runList);
    // fixture.detectChanges();
    expect(component.dealerBlock, 'false').to.be.false;
});

it('should show DealerBlock Component exist', () => {
    expect(component.Dealer);
    expect(component.dealerBlock).to.be.true;
});

错误:  VehicleSaleInfoComponent     ×应该显示RunList组件       PhantomJS 2.1.1(Windows 8 0.0.0)     false:预期true为假

它不会选择方法中的值...任何在方法中获取值的方法。

1 个答案:

答案 0 :(得分:0)

上面的规范没有测试方法,所以它们失败了。应调用方法以进行测试:

  it('should Show RunList Component exist', () => {
    component['dealerBlock'] = null; // avoid asserted type check
    component.runList();
    expect(component.dealerBlock).to.be.false;
  });