gpt4 book ai didi

javascript - 追加到 Jasmine 中的 'IT'描述名

转载 作者:行者123 更新时间:2023-11-29 15:25:32 25 4
gpt4 key购买 nike

我一直试图在运行时更改我的“IT”描述,虽然知道 Protractor 是异步的,但我仍然觉得我遗漏了一些东西。

describe('Describe something', function() {
var testParams = [1,2,3,4,5,6,7,8,9,10];
var testVar;
beforeEach( function() {
// ...
testVar = "Eyooo";
});

for (var i = 0; i < testParams.length; i++) {
(function (testSpec) {
// ...

it('should do something '+testVar , function () {
//...
console.log(testVar);

});

// ...
})(testParams[i]);

};

});

这部分我一直有问题

it('should do something '+testVar , function () {

知道案例是异步处理的,我一直在努力弄清楚何时加载“it”描述。

当我运行上面的代码时,这是我的输出

Describe something                                                                                                                                              
√ should do something undefined
Eyooo
√ should do something undefined
Eyooo

所以结果很明显,但我一直很难理解这个问题。我想这违反了行业标准,但它确实有助于我的案例。

我也试过类似的东西..

it('should do something', function() {    
testVar = "Eyooo";
});

it('should do something '+testVar, function () {
//...
console.log(testVar);
});

没用

欢迎任何意见!

最佳答案

这是一个很好的问题。不得不集思广益!

您发现了错误的根本原因。它表现异步我看到的动态构造 "IT" block 描述的解决方案是不通过 BeforeEach() 而是调用自定义函数来分配名称

请检查您引用但修改以构建 IT 描述的相同示例

describe('Describe something', function() {
var testParams = [1,2,3,4,5,6,7,8,9,10];
var testVar;
for (var i = 0; i < testParams.length; i++) {
(function (testSpec) {
// ...

it(getName(), function () {
//...
console.log(testVar);

});

// ...
})(testParams[i]);

};
function getName(){
testVar = "Eyooo";
return 'should do something '+testVar
}
});

另一个例子,不是在另一个IT block 中编写构建描述的逻辑,而是在另一个自定义函数中构建并调用它

describe('Describe something', function() {
var testVar
it(getName(), function () {
//...
console.log(testVar);
});
function getName(){
testVar = "Eyooo";
return 'should do something '+testVar
}
});

关于javascript - 追加到 Jasmine 中的 'IT'描述名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39635400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com