【问题标题】:unit test If statement angular6单元测试If语句angular6
【发布时间】:2019-02-22 00:29:54
【问题描述】:

我在angular6中有一个简单的if语句,我需要为此编写单元测试

  ngOnInit() {
    if(this.actionResult){
      this.dataTable = JSON.parse(JSON.parse(this.actionResult));
      this.dataSource = new MatTableDataSource(this.dataTable);
      this.dataSource._updateChangeSubscription();
    }        
  }

代码覆盖率显示if path not taken,我如何在我的 spec.ts 中覆盖它。

请帮忙。

【问题讨论】:

    标签: angular unit-testing


    【解决方案1】:

    还有一些断言,比如

    • dataTabletoBeTruthy()/toBeDefined()
    • dataSource 也是如此。
    • spy 在方法 _updateChangeSubscription 上并期望它已被调用

    此断言将使您的代码充分证明。

    请参阅我对其他问题的回答here 以供参考。

    【讨论】:

      猜你喜欢
      • 2016-08-16
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      • 2016-02-04
      • 2018-10-31
      • 2021-06-10
      • 2021-04-23
      相关资源
      最近更新 更多