我是新来的人。请帮助我进行redux-saga / effecs的单元测试。 可以说我对单独的功能具有Redux效果
export function* appEffects() {
yield all([
takeEvery(Types.CONFIGURE_LANGUAGE, configureLanguage),
// other effects for this feature (removed to simplify example)
]);
}
function* configureLanguage(action: Actions.ConfigureLanguageAction) {
const currentLanguage: string = 'en'; // simplified example
yield put({ type: Types.SET_CURRENT_LANGUAGE, payload: currentLanguage });
}
我可以使用以下代码直接测试configureLanguage
import { runSaga } from 'redux-saga';
async function recordSaga(saga: any, initialAction: any) {
const dispatched: any = [];
await runSaga(
{ dispatch: (action: any) => dispatched.push(action) },
saga,
initialAction
).toPromise;
return dispatched;
}
it('should configure language', async () => {
const action = appActions.service.configureLanguage('en');
const dispatched = await recordSaga(
configureLanguage
action
);
expect(dispatched).toEqual([{ type: Types.SET_CURRENT_LANGUAGE, payload: 'en' }]);
});
如何从configureLanguage
测试appEffects
?如果我尝试
const dispatched = await recordSaga(
appEffects
action
);
我收到dispatched === []
答案 0 :(得分:0)
您没有调用toPromise
方法来返回诺言-现在您正在await
中使用toPromise
方法本身,因此recordSaga
在{{1 }}完成,因此runSaga
仍然是空的。
dispatched