我有下一个传奇功能
function* fetchData() {
try {
const response = yield call(() => api.get('/names'));
yield put(actions.succeeded(response.data));
} catch (e) {
yield put(actions.failed(e.message));
} finally {
yield delay(3000);
yield put(actions.fetchGraph());
}
}
如何在finally块中测试yield put(actions.fetchGraph());
?
答案 0 :(得分:1)
我没有对其进行测试,但在iterator().next
调用iterator.throw
块的结果后不应该是finally
?
const iterator = fetchData()
assert.deepEqual(
iterator.next().value,
call(api.get, '/names'),
"fetchData should yield an Effect call(api.get, '/names')",
)
const error = { message: 'Fake Error' }
// Throw error and test `catch`
assert.deepEqual(
iterator.throw(error).value,
put(actions.failed(error.message)),
"fetchData should yield an Effect put(actions.failed(error.message))",
)
// After `catch` block, `next()` should continue in `finally`
assert.deepEqual(
iterator.next().value,
delay(3000),
"fetchData should yield an Effect delay(3000)",
)