在Mocha硒网页测试中嵌套forEach失败



我正在编写一个测试脚本,应该执行以下操作(这是一个示例,但逻辑和结构相同(。

  • 对于arr1中的每个项,调用函数arr_func_1
  • 在arrfunc_1中,记录当前项,然后对于arr2中的每个项,调用函数arrfunc_2
  • 在arr_func_2中,记录当前项目

调用被包装在its((中,因为如果数组中的一个元素失败,那么它需要正常地失败并继续到数组中的下一个元素。

预期结果应该是:

11020302.1020303.102030

相反,我收到1.2.3

这让我相信初始函数是异步调用的。

var arr1 = [1,2,3]
var arr2 = [10,20,30]
function arr_func_1(item){
console.log(item);
arr2.forEach(function(item){
it('should loop through arr2, function(){
arr_func_2(item);
})
});
}
function arr_func_2(item){
console.log(item);
}
describe('test case', function(){
arr1.forEach(function(item){
it('should loop through array 1', function(){   
arr_func_1(item);
})
}
})

测试应该在运行之前定义。should loop through arr2测试是在should loop through array 1运行时定义的,因此在测试运行期间会忽略它们。

除了循环,这类似于:

describe('test case', function(){
it('should loop through array 1', function(){   
it('should loop through arr2, function(){})
})
})

CCD_ 3块不应该在另一个CCD_。

可能需要:

describe('test case', function(){
for (const item1 of arr1) {
it('should loop through array 1', function(){...});
for (const item2 of arr2) {
it('should loop through array 2', function(){...});
}
}
});

最新更新