为什么 JS 函数在空手道框架中只调用一次,如果应该多次调用?
Why JS functions are called only once in Karate framework if should several times?
这是我的测试场景
Scenario: build json using js function
* def x = read('classpath:data/user.json')
* def body = { updated : [], deleted : [] };
* def fun =
"""
function(n){
for(i=0; i<n; i++) {
x.email = 'api.test+' + Math.random() + '@cii.io';
body.updated.add(x);
}
}
"""
* eval fun(3)
* copy body = body
* print body
我的期望是我将有 3 个实体和 3 个独特的电子邮件,因为每次都会调用 Math.random()。
但我在结果中看到下一个
{
"updated": [
{
"email": "api.test+0.5327150054220268@cii.io",
"businessUnit": "DE"
},
{
"email": "api.test+0.5327150054220268@cii.io",
"businessUnit": "DE"
},
{
"email": "api.test+0.5327150054220268@cii.io",
"businessUnit": "DE"
}
],
"deleted": [
]
}
我想知道我在这里做错了什么?
这对我来说很好用:
* def tmp = []
* def fun = function(n){ for(var i = 0; i < n; i++) tmp.add(i) }
* eval fun(3)
* print tmp
这是我的测试场景
Scenario: build json using js function
* def x = read('classpath:data/user.json')
* def body = { updated : [], deleted : [] };
* def fun =
"""
function(n){
for(i=0; i<n; i++) {
x.email = 'api.test+' + Math.random() + '@cii.io';
body.updated.add(x);
}
}
"""
* eval fun(3)
* copy body = body
* print body
我的期望是我将有 3 个实体和 3 个独特的电子邮件,因为每次都会调用 Math.random()。 但我在结果中看到下一个
{
"updated": [
{
"email": "api.test+0.5327150054220268@cii.io",
"businessUnit": "DE"
},
{
"email": "api.test+0.5327150054220268@cii.io",
"businessUnit": "DE"
},
{
"email": "api.test+0.5327150054220268@cii.io",
"businessUnit": "DE"
}
],
"deleted": [
]
}
我想知道我在这里做错了什么?
这对我来说很好用:
* def tmp = []
* def fun = function(n){ for(var i = 0; i < n; i++) tmp.add(i) }
* eval fun(3)
* print tmp