首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Redux Saga测试计划库测试延迟函数的redux-saga时出错

使用Redux Saga测试计划库测试延迟函数的redux-saga时出错
EN

Stack Overflow用户
提问于 2019-05-27 19:32:49
回答 1查看 2.6K关注 0票数 1

我试图通过使用Redux测试计划库来测试我的Redux函数,而且由于我的saga中的延迟函数,我被困住了。

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

saga.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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();
    });
});

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

代码语言:javascript
复制
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)时,它会抛出此错误

代码语言:javascript
复制
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));,它将显示以下不同的错误

代码语言:javascript
复制
Error: call: argument of type {context, fn} has undefined or null `fn`

你能不能帮我试一试我的传奇故事?我不想为了通过测试而删除代码中的延迟语句。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-31 21:46:51

您可以简单地模拟calldelay在引擎盖下使用它。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56331569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档