javascript - 如何使用 redux-saga-test-plan 测试所有效果。所有效果不匹配
问题描述
我有一个生成器,我想用单元测试覆盖
export default function* gatawayFlow() {
yield all([
takeEvery(actionTypes.GET_GATEWAYS_REQUEST, getGatewaysFlow),
takeLatest(actionTypes.SELECT_GATEWAY_REQUEST, selectGatewayFlow),
]);
}
我用 redux-sag-test-plan 写了一个简单的测试
import {expectSaga, testSaga} from 'redux-saga-test-plan';
import gatawayFlow, {getGatewaysFlow, selectGatewayFlow} from '../logic/sagas';
import * as actions from '../logic/actions';
import * as actionTypes from '../logic/actionTypes';
import {takeEvery, takeLatest} from '@redux-saga/core/effects';
// Unit-test
describe('Unit tests', () => {
test('Test all effect', () => {
const saga = testSaga(gatawayFlow);
saga
.next()
.all({
[actionTypes.GET_GATEWAYS_REQUEST]: takeEvery(actionTypes.GET_GATEWAYS_REQUEST, getGatewaysFlow),
[actionTypes.SELECT_GATEWAY_REQUEST]: takeLatest(actionTypes.SELECT_GATEWAY_REQUEST, selectGatewayFlow)
})
.next()
.isDone();
// expect(gatawayFlow().next().value).toEqual(all([
// takeEvery(actionTypes.GET_GATEWAYS_REQUEST, getGatewaysFlow),
// takeEvery(actionTypes.SELECT_GATEWAY_REQUEST, selectGatewayFlow),
// ])); ---> THIS TEST WORKS CORRECT
});
});
而我的测试没有通过。我的终端中有这个错误。有什么想法可以解决吗?
解决方案
您应该使用all([...effects]) - 并行效果而不是表单的字典对象{label: effect, ...}
saga.ts
:
import { all, takeEvery, takeLatest } from 'redux-saga/effects';
export const actionTypes = {
GET_GATEWAYS_REQUEST: 'GET_GATEWAYS_REQUEST',
SELECT_GATEWAY_REQUEST: 'SELECT_GATEWAY_REQUEST',
};
export function* getGatewaysFlow() {}
export function* selectGatewayFlow() {}
export default function* gatawayFlow() {
yield all([
takeEvery(actionTypes.GET_GATEWAYS_REQUEST, getGatewaysFlow),
takeLatest(actionTypes.SELECT_GATEWAY_REQUEST, selectGatewayFlow),
]);
}
saga.test.ts
:
import { expectSaga, testSaga } from 'redux-saga-test-plan';
import gatawayFlow, { actionTypes, getGatewaysFlow, selectGatewayFlow } from './saga';
import { takeEvery, takeLatest } from '@redux-saga/core/effects';
describe('Unit tests', () => {
test('Test all effect', () => {
const saga = testSaga(gatawayFlow);
saga
.next()
.all([
takeEvery(actionTypes.GET_GATEWAYS_REQUEST, getGatewaysFlow),
takeLatest(actionTypes.SELECT_GATEWAY_REQUEST, selectGatewayFlow),
])
.next()
.isDone();
});
});
测试结果:
PASS redux-saga-examples packages/redux-saga-examples/src/stackoverflow/69252089/saga.test.ts
Unit tests
✓ Test all effect (2 ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 75 | 100 | 20 | 100 |
saga.ts | 75 | 100 | 20 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 3.553 s
Ran all test suites related to changed files.
推荐阅读
- matlab - 如何使用 Matlab 加速非常长的表达式计算?
- python - 在 Python 中将数组与精确值和近似值进行比较
- html - CSS 文件没有反映
- arrays - 具有嵌套 ID 数组的 Mongo 聚合
- python-3.x - pandas_ml 坏了吗?
- php - SQLSTATE[HY000] [1045] 用户 'username_a'@'localhost' 的访问被拒绝
- gradle - 只为所需平台构建带有二进制文件的 jar (javacpp)
- oracle - Oracle PL/SQL 中的校验和
- android - 为什么权限 Manifest.permission.ACCESS_FINE_LOCATION 没有激活“位置”选项?
- c# - MacOSX 的独立文件浏览器