为什么JS函数只在Karate框架中调用一次,如果应该多次?

问题描述 投票:1回答:1

这是我的测试场景

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": [
  ]
}

我想知道我在这里做错了什么?

function karate
1个回答
1
投票

这对我来说很好:

* def tmp = []
* def fun = function(n){ for(var i = 0; i < n; i++) tmp.add(i) }
* eval fun(3)
* print tmp
© www.soinside.com 2019 - 2024. All rights reserved.