为什么在测试时此javascript数组未返回true?

时间:2020-06-26 15:17:04

标签: javascript

更新:

运行typeof managers表明它是一个对象。

在测试管理器是否为数组时,测试将失败。如果isManager属性为true,则将所有管理器对象都推入此管理器数组。

该函数正在过滤管理器,并将对象放入数组中,但测试返回该数组不是有效数组。

我在这里想念什么?括号?当我将对象推入数组时,数组的状态会改变吗?

CODE

userUtils.getManagers = function() {
  var managers = [];
  for (const manager of users) {
    if (manager.isManager === true) {
      managers.push(manager);
      let name = manager.name.first + ' ' + manager.name.last;
      console.log(name);
    }
    // console.log(managers);
  }
};

错误

● Problem 2 - userUtils.getManagers() › userUtils.getManagers() should return a proper Array

    expect(received).toBe(expected) // Object.is equality

    Expected: true
    Received: false

      12 |   test('userUtils.getManagers() should return a proper Array', function() {
      13 |     let managers = userUtils.getManagers();
    > 14 |     expect(Array.isArray(managers)).toBe(true);
         |                                     ^
      15 |     expect(managers.length).toBe(69);
      16 |   });
      17 | 

      at Object.<anonymous> (src/problem-2.test.js:14:37)

  ● Problem 2 - userUtils.getManagers() › userUtils.getManagers() should only contain Manger users

    TypeError: Cannot read property 'forEach' of undefined

      19 |     let managers = userUtils.getManagers();
      20 | 
    > 21 |     managers.forEach(manager => {
         |              ^
      22 |       // First, check the isManager property
      23 |       expect(typeof manager.isManager).toBe('boolean');
      24 |       expect(manager.isManager).toBe(true);

      at Object.<anonymous> (src/problem-2.test.js:21:14)

该元素的外观:

 * {
 *   "id": 1,                               // A unique Number
 *   "name": {                              // An Object with user names
 *     "first": "Paige",
 *     "last": "Bools"
 *   },
 *   "birthDate": "1995-02-04T07:34:45Z",   // A Date in String form
 *   "contact": {                           // An Object with contact info
 *     "phone": "8989068955",
 *     "email": "pbools0@webmd.com"
 *   },
 *   "address": {                           // An Object with address info
 *     "street": "476 Veith Parkway",
 *     "city": "Cuamba",
 *     "country": "Mozambique"
 *   },
 *   "accessCount": 776,                    // A Number: access count to the app
 *   "isManager": false                     // A Boolean: is this a manager?
 * }
 *

1 个答案:

答案 0 :(得分:1)

if __name__ == '__main__': def func(elem): time.sleep(0.5) return elem def callback(elem): # do something with processed data pass queue = multiprocessing.Queue() for i in range(0, 10000): queue.put(i) process_pool = multiprocessing.Pool(processes=num_processes, maxtasksperchild=1) results = [] while True: data = queue.get(True) if data is None: break res = process_pool.apply_async(func=func, args=(data,), callback=callback) results.append(res) flag = False for i, res in enumerate(results): try: res.wait(600) # do some logging results[i] = None except TimeoutError: flag = True # do some logging process_pool.close() if flag: process_pool.terminate() process_pool.join() # done! 不返回任何内容。您需要在函数末尾添加userUtils.getManagers;否则,如果您不返回任何内容,则会隐式返回return managers的返回值,并导致undefined

TypeError: Cannot read property 'forEach' of undefined