【问题标题】:How To call/check values inside a method in UnitTest Angular4如何在 UnitTest Angular4 中调用/检查方法内的值
【发布时间】:2018-05-13 02:02:46
【问题描述】:

我的 .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;
});

错误: 车辆销售信息组件 × Show RunList 组件是否存在 PhantomJS 2.1.1 (Windows 8 0.0.0) false:预期 true 为 false

它不会选择方法内的值...任何方法来获取方法内的值。

【问题讨论】:

  • 是这样写的.. it('应该显示RunList组件存在', () => { component.dealerBlock = false; expect(component.runList); // fixture.detectChanges();期望(component.dealerBlock, 'false').to.be.false; });但我不知道它的正确方式......
  • expect(component.runList) 应该做什么?
  • expect(component.runList).to.be.exist;

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


【解决方案1】:

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

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

【讨论】:

  • 好的,谢谢,否则我们可以这样使用...?正确的 ..? it('应该显示 RunList 组件是否存在', () => { component.dealerBlock = false; component.runList(); expect(component.dealerBlock).to.be.false; });
  • 并非如此。由于您将其设置为component.dealerBlock = false,因此不清楚该方法是否有作用。这就是它最初设置为 null 的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-16
  • 2011-07-25
  • 1970-01-01
  • 2021-10-14
相关资源
最近更新 更多