从角度函数中删除副作用

时间:2017-07-14 23:01:59

标签: javascript angular unit-testing typescript side-effects

我正在创建一个接收两个参数optionsdata的方法,但是在返回值中只返回options,我想在这个项目上实现单元测试,我想要为了避免副作用对这个方法进行测试,我发现女巫是做到这一点的最佳方法,返回一个数组,每个数组有两个值,或者用两个小方法分割这个方法返回不同的值。

这是我简化演示的方法:

private setEst(data, options): object {

    const numCols: number = data.getColNumber();    
    const indxRows: Array<Number> = data.getIndexRows();
    const otherArray: : object[] = [];

    // Creating options output that will be returned
    for (let col = 1; col < numCols; col++) {
        // Do something with options to modify the value of this param
        ...

        // Read data to create an array that will be used by the next process
        otherArray.push({
          id: data(col).getId();
        });
      }
    }

    // Output data as a side effect
    indxRows.forEach(indx => {
      for (let col = 1; col < numCols; col++) {

        // Do something with data object and otherArray const, 
        // data is modified and returned as side effect...
        ...


      }
    });

    return options;
}

0 个答案:

没有答案