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