这是我的测试场景
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": [
]
}
我想知道我在做什么错吗?
答案 0 :(得分:1)
这对我来说很好:
* def tmp = []
* def fun = function(n){ for(var i = 0; i < n; i++) tmp.add(i) }
* eval fun(3)
* print tmp