关于javascript:通过使用Redux Saga测试计划库测试具有延迟功能的redux-saga时出错

Got an error while testing redux-saga with delay functions by using Redux Saga Test Plan library

我正在尝试通过使用Redux Saga测试计划库来测试我的redux-saga函数,由于我的saga中的延迟函数,我被卡住了。

如果我删除该行,则yield delay(1000)所有测试均通过且没有任何错误。

saga.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
export function* addWorkoutSaga({ payload }) {
    try {
        yield put(beginAjaxCall());

        yield delay(1000);
        yield call(WorkoutService.add, payload);

        yield call(toast.success,"Item added successfully.");
        yield put(closeModal(Modal.AddWorkout));
        yield put(addWorkout.success());
        yield call(fetchWorkoutsSaga);
    }
    catch (error) {
        console.log(error)
        yield put(addWorkout.failure({ errorMessage: error.statusText }));
        yield call(toast.error,"Error occured.  Please try again.");
    }
}

saga.test.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import {
    call,
    put,
    //takeLatest,
    delay
} from 'redux-saga/effects';
import * as matchers from 'redux-saga-test-plan/matchers';
import { expectSaga } from 'redux-saga-test-plan';
import { throwError } from 'redux-saga-test-plan/providers';
import {
    fetchWorkouts,
    addWorkout,
    //editWorkout,
    deleteWorkout
} from '../../actions/workoutApiActionsForSaga';
import { WorkoutService } from"../../services";
import {
    fetchWorkoutsSaga,
    deleteWorkoutSaga,
    addWorkoutSaga
} from '../workouts.saga'

describe('testing Workouts Sagas with redux-saga-test-plan', () => {

    const fakeAddPayload = {
        payload: {
            id: '6e8dbbc8-233f-41b1-ade3-ca568b35918c',
            date: '2019-05-27T18:10:35.282Z',
            workoutType: 'Running',
            calories: 100
        }
    };

    const errorToThrow = {
        statusText: 'custom Error Message'
    };    

    it('should call addWorkoutSaga function', () => {
        return expectSaga(addWorkoutSaga, fakeAddPayload)
            .provide([
                [matchers.call.fn(delay), null],
                [matchers.call.fn(WorkoutService.add), null],                
                [matchers.call.fn(fetchWorkoutsSaga), null]
            ])
            .call(WorkoutService.add, fakeAddPayload.payload)
            .put(addWorkout.success())
            .call(fetchWorkoutsSaga)
            .run();
    });
});

运行测试时,由于预期值不等于实际值,因此出现以下错误。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Expected
    --------
    { '@@redux-saga/IO': true,
      combinator: false,
      type: 'CALL',
      payload:
       { context: null,
         fn: [Function: add],
         args:
          [ { id: '6e8dbbc8-233f-41b1-ade3-ca568b35918c',
              date: '2019-05-27T18:10:35.282Z',
              workoutType: 'Running',
              calories: 100 } ] } }

    Actual:
    ------
    1. { '@@redux-saga/IO': true,
      combinator: false,
      type: 'CALL',
      payload: { context: null, fn: [Function: delayP], args: [ 1000 ] } }

      at new SagaTestError (node_modules/redux-saga-test-plan/lib/shared/SagaTestError.js:17:57)
      at node_modules/redux-saga-test-plan/lib/expectSaga/expectations.js:67:13
      at node_modules/redux-saga-test-plan/lib/expectSaga/index.js:563:7
          at Array.forEach ()
      at checkExpectations (node_modules/redux-saga-test-plan/lib/expectSaga/index.js:562:18)

在我看来,该错误与delay函数有关。 当我尝试将延迟函数更改为yield call(delay, 1000)时,它将引发此错误

1
Error: instead of writing `yield call(delay, 1000)` where delay is an effect from `redux-saga/effects` you should write `yield delay(1000)`

如果将行更改为yield call(delay(1000));,则显示以下不同错误

1
Error: call: argument of type {context, fn} has undefined or null `fn`

您能帮我延迟测试我的传奇吗? 我不想删除代码中的delay语句以使测试通过。


您可以简单地模拟delay在引擎盖下使用的call

如此处进一步详细描述:https://github.com/jfairbank/redux-saga-test-plan/issues/257