ExpectSaga put 断言因多个 put 效果而失败
ExpectSaga put assertion failes with multiple put effects
传奇有多个放置。
export function* changeItemsSaga(action) {
const prerequisite1 = yield select(prerequisite1Selector);
// some processing
yield put(actions.myAction1(payload1));
// some more processing
const prerequisite2 = yield select(prerequisite2Selector);
// some more processing
yield put(actions.myAction2(payload2));
}
当 saga 返回多重效果时,我该如何写我的看跌预期?
it('should update items', () =>
expectSaga(sagas.changeItemsSaga, action)
.provide([
[select(prerequisite1), {}],
[select(prerequisite2), {}],
])
.put([actions.myAction1(payload1), actions.myAction2(payload2)])
.run());
ExpectSaga单元测试returns出现如下错误:
Saga test error:
put expectation unmet:
Expected
--------
{ channel: null,
action:
[ { type: 'MY_ACTION_1',
payload: { myItem1: [Object] } },
{ type: 'MY_ACTION_2',
payload: { itemCollection: [Object] } } ] }
Actual:
------
1. { channel: null,
action:
{ type: 'MY_ACTION_1',
payload:
{ myItem1:
{ index: 0,
childItem1: [Object],
childItem2: [Object] } } } }
2. { channel: null,
action:
{ type: 'MY_ACTION_2',
payload: { itemCollection: [ [Object] ] } } }
这个也不行。
it('should update items', () =>
expectSaga(sagas.changeItemsSaga, action)
.provide([
[select(prerequisite1), {}],
[select(prerequisite2), {}],
])
.put(actions.myAction1(payload1))
.put(actions.myAction2(payload2))
.run());
是returns这个错误。
Saga test error:
put expectation unmet:
Expected
--------
{ channel: null,
action:
{ type: 'MY_ACTION_1',
payload:
{ myItem1:
{ index: 0,
childItem1: [Object],
childItem2: [Object] } } } }
Actual:
------
1. { channel: null,
action:
{ type: 'MY_ACTION_1',
payload:
{ myItem1:
{ index: 0,
childItem1: [Object],
childItem2: [Object] } } } }
2. { channel: null,
action:
{ type: 'MY_ACTION_2',
payload: { itemCollection: [ [Object] ] } } }
provide
语法允许您测试 saga 的最终效果,而不是引导所有状态。
这是来自 redux-saga docs -
的示例
test('test only final effect with .provide()', () => {
/*
* With the .provide() method from expectSaga
* you can by pass in all expected values
* and test only your saga's final effect.
*/
return expectSaga(callApi, 'url')
.provide([
[select(selectFromState), selectedValue],
[call(myApi, 'url', selectedValue), response]
])
.put(success(response))
.run();
});
通过调用actions
调用2x,连续测试运行,有效跳过sagas.changeItemsSaga
中间的put
[=17] =]
看起来您可能要使用的函数是 testSaga,它将让您更细致地了解效果流。
传奇有多个放置。
export function* changeItemsSaga(action) {
const prerequisite1 = yield select(prerequisite1Selector);
// some processing
yield put(actions.myAction1(payload1));
// some more processing
const prerequisite2 = yield select(prerequisite2Selector);
// some more processing
yield put(actions.myAction2(payload2));
}
当 saga 返回多重效果时,我该如何写我的看跌预期?
it('should update items', () =>
expectSaga(sagas.changeItemsSaga, action)
.provide([
[select(prerequisite1), {}],
[select(prerequisite2), {}],
])
.put([actions.myAction1(payload1), actions.myAction2(payload2)])
.run());
ExpectSaga单元测试returns出现如下错误:
Saga test error:
put expectation unmet:
Expected
--------
{ channel: null,
action:
[ { type: 'MY_ACTION_1',
payload: { myItem1: [Object] } },
{ type: 'MY_ACTION_2',
payload: { itemCollection: [Object] } } ] }
Actual:
------
1. { channel: null,
action:
{ type: 'MY_ACTION_1',
payload:
{ myItem1:
{ index: 0,
childItem1: [Object],
childItem2: [Object] } } } }
2. { channel: null,
action:
{ type: 'MY_ACTION_2',
payload: { itemCollection: [ [Object] ] } } }
这个也不行。
it('should update items', () =>
expectSaga(sagas.changeItemsSaga, action)
.provide([
[select(prerequisite1), {}],
[select(prerequisite2), {}],
])
.put(actions.myAction1(payload1))
.put(actions.myAction2(payload2))
.run());
是returns这个错误。
Saga test error:
put expectation unmet:
Expected
--------
{ channel: null,
action:
{ type: 'MY_ACTION_1',
payload:
{ myItem1:
{ index: 0,
childItem1: [Object],
childItem2: [Object] } } } }
Actual:
------
1. { channel: null,
action:
{ type: 'MY_ACTION_1',
payload:
{ myItem1:
{ index: 0,
childItem1: [Object],
childItem2: [Object] } } } }
2. { channel: null,
action:
{ type: 'MY_ACTION_2',
payload: { itemCollection: [ [Object] ] } } }
provide
语法允许您测试 saga 的最终效果,而不是引导所有状态。
这是来自 redux-saga docs -
的示例test('test only final effect with .provide()', () => {
/*
* With the .provide() method from expectSaga
* you can by pass in all expected values
* and test only your saga's final effect.
*/
return expectSaga(callApi, 'url')
.provide([
[select(selectFromState), selectedValue],
[call(myApi, 'url', selectedValue), response]
])
.put(success(response))
.run();
});
通过调用actions
调用2x,连续测试运行,有效跳过sagas.changeItemsSaga
中间的put
[=17] =]
看起来您可能要使用的函数是 testSaga,它将让您更细致地了解效果流。