gpt4 book ai didi

javascript - ES6 [Symbol.iterator] 不是带数组的函数

转载 作者:行者123 更新时间:2023-11-29 23:56:16 25 4
gpt4 key购买 nike

我正在尝试测试这个按预期执行的 redux saga 生成器:

export function* mygen(id) {
let selectedItems = yield select(myselector) //return an array of integer
for(let item of selectedItems) {
const {response, error} = yield call(asynFunc, id, item)
if (response) {
yield put({type: ACTION_SUCCESS, data: response.data})
}
else {
yield put({type: ACTION_FAILURE, error: error.response.data.error})
}
}
}

这是我的测试:

it("myTest", () => {
const gen = mygen()
expect(gen.next().value).to.deep.equal(select(myselector))

expect(gen.next({selectedItems: [1], item:1, id:1})).to.deep.equal(call(asynFunc, 1, 1))

})

我遇到了这个错误:

 TypeError: selectedItems[Symbol.iterator] is not a function

这很奇怪,因为 selectedItems 是一个数组

你知道如何测试它吗?
提前致谢

最佳答案

稍作调整即可通过测试:

import Promise from 'bluebird';
import { delay } from 'redux-saga';
import { call, select } from 'redux-saga/effects';

import { logger } from '../utils';

const name = '21/Test async function';
const log = logger(name);

const delayTime = 10;

const reducer = () => ({ selectedItems: [ 1 ], item: 1, id: 1 });

const myselector = item => item.selectedItems;

const asynFunc = (id, item) => delay(delayTime, {response: { selectedItems: [ 1 ], item: item, id: id }});

export function* mygen(id) {
let selectedItems = yield select(myselector); //return an array of integer
// log('selectedItems', selectedItems);
for(let item of selectedItems) {
// log('item', item);
const {response, } = yield call(asynFunc, id, item);
// log('response', response);
}
}


export default {
name,
saga: () => mygen(1),
reducer: reducer,
useThunk: !true,
execute(store) {
const gen = mygen();

const value = gen.next().value;
log('gen.next().value', value);
log('select(myselector)', select(myselector));
log('gen.next().value === select(myselector)', JSON.stringify(value) === JSON.stringify(select(myselector)));

const selectedItems = [1];
const response = gen.next(selectedItems).value;
const asyncCallResult = call(asynFunc, 1, 1);
log(`gen.next([1]).value`, response);
log(`call(asynFunc, 1, 1)`, asyncCallResult);
log(`gen.next([1]).value === call(asynFunc, 1, 1)`, JSON.stringify(asyncCallResult) === JSON.stringify(asyncCallResult));

return delay(2 * delayTime, this);
}
};

日志看起来像:

   00000000: [Runner] ---------- running example 21/Test async function
00000003: [Runner] store initial state Object {selectedItems: Array[1], item: 1, id: 1}
00000010: [21/Test async function] gen.next().value Object {@@redux-saga/IO: true, SELECT: Object}
00000011: [21/Test async function] select(myselector) Object {@@redux-saga/IO: true, SELECT: Object}
00000011: [21/Test async function] gen.next().value === select(myselector) true
00000012: [21/Test async function] gen.next([1]).value Object {@@redux-saga/IO: true, CALL: Object}
00000012: [21/Test async function] call(asynFunc, 1, 1) Object {@@redux-saga/IO: true, CALL: Object}
00000013: [21/Test async function] gen.next([1]).value === call(asynFunc, 1, 1) true
* 00000088: [Runner] store final state Object {selectedItems: Array[1], item: 1, id: 1}
00000089: [Runner] ---------- example 21/Test async function is done

对于选择器myselector = item => item,selected items 是一个对象并且TypeError: selectedItems[Symbol.iterator] is not a function被抛出:

case 2:
selectedItems = _context.sent;
//return an array of integer
// log('selectedItems', selectedItems);
_iteratorNormalCompletion = true;
_didIteratorError = false;
_iteratorError = undefined;
_context.prev = 6;
_iterator = selectedItems[Symbol.iterator](); // selectedItems is not an array

关于javascript - ES6 [Symbol.iterator] 不是带数组的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41586701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com