使用 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

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

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();
    });
});

当我运行测试时,我得到了以下错误,因为期望值不等于实际值。

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 (<anonymous>)
      at checkExpectations (node_modules/redux-saga-test-plan/lib/expectSaga/index.js:562:18)

在我看来,错误与 delay 功能有关。当我尝试将延迟函数更改为 yield call(delay, 1000) 时,它会抛出此错误

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));,它会显示以下不同的错误

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

能否请您帮助我如何延迟测试我的 saga?我不想删除代码中的延迟语句来使测试通过。

您可以简单地模拟 calldelay 在幕后使用它。

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