【问题标题】:How to write test case for switch case in Angular for following case如何在Angular中为以下案例编写开关案例的测试用例
【发布时间】:2021-03-24 07:33:12
【问题描述】:

这是 Angular 函数代码,我使用 switch(true) 和 switch case 作为条件来检查测试值

  yAxisText() {
    const KilobyteConst = this.KilobyteConst;
    const MegabyteConst = this.MegabyteConst;
    // to rotate text on x axis
    d3.selectAll('g.axis-y>g.tick>text')
    .each(function(d: number, i) {
      let textValue, calculation;
      switch (true) {
        case (d > KilobyteConst && d < MegabyteConst):
          calculation = d / KilobyteConst;
          break;
        case (d > MegabyteConst):
          calculation = d / MegabyteConst;
          break;
        default:
          calculation = d;
      }

      textValue = `${d3.format(',.0f')(calculation)} <title>${d3.format('.2f')(calculation)}</title>`;
      console.log('test on ', textValue, d, i);
      d3.select(this)
      .style('font-size', '12px')
      .style('color', '#525252')
      .style('cursor', 'pointer')
      .html(textValue);
    });
  }

为了更清晰,我添加了一张图片 Angular function with switch cases

【问题讨论】:

    标签: javascript angular unit-testing testing jasmine


    【解决方案1】:

    您不应该使用switch,使用 if/else 进行测试会更简单:

    let textValue, calculation;
           if(d > KilobyteConst && d < MegabyteConst) {
              calculation = d / KilobyteConst;
           } else if(d > MegabyteConst)
              calculation = d / MegabyteConst;
           } else {
              calculation = d;
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      • 2020-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      相关资源
      最近更新 更多