代码之家  ›  专栏  ›  技术社区  ›  nhrcpt

在量角器的循环内执行多个it块并使用it块中的变量

  •  0
  • nhrcpt  · 技术社区  · 6 年前

       for (i=0; i <3; i++){
        it ('should complete the first test', function(){
            let j = i+1;
            console.log(j);
    
        });
    
        it ('should complete the first test', function(){
            let K = i+3;
            console.log(K);
    
        });
    }
    

    4
    .6
    .4
    .6
    .4
    .6
    .
    

    但是我需要第一个试块的值为1,2,3,第二个试块的值为3,4,5。我看到有很多关于循环问题的问题,但无法找到一个可以帮助我的解决方案。如果我将函数包装为“returnit(…”,这只适用于第一个it块。有没有办法解决这个问题?

    2 回复  |  直到 6 年前
        1
  •  1
  •   DublinDev    6 年前

    如果在循环上方声明计数器,则应获得所需的计数。

    describe('desribe the test', () => {
        let count = 1;
    
        for (i=0; i <3; i++){
            it ('should complete the first test', function(){
                console.log(count);
                count++;
            });    
        }
        for (i=0; i <3; i++){
            it ('should complete the first test', function(){
                console.log(count);
                count++;
            });    
        }
    })
    
        2
  •  0
  •   Jason Spradlin    6 年前

    it(...) 只需注册测试。你的 for 块将循环三次,记录六个测试。完成for block后,运行 i === 3 ,只有这样你才能 它(…) 块运行,并记录其中一个的值 i+1 i+3 . 在所有情况下, i 3 所以 i+1 = 6