首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用redux-saga-test-plan,我有一个将函数作为参数的分派操作。这使得测试总是失败。

使用redux-saga-test-plan,我有一个将函数作为参数的分派操作。这使得测试总是失败。
EN

Stack Overflow用户
提问于 2021-05-05 23:03:29
回答 1查看 287关注 0票数 0

我有一个能产生put的传奇故事。put的一个参数是一个函数对象。

我只复制了相关的saga函数:

代码语言:javascript
复制
function* updateTaskTimeCancelledTimeRejectedSuccess(action) {
    const {payload} = action.data
    const tasks = yield select(getTasksSelector);
    const parent = yield call(findExistingTaskParent, tasks, action.data.taskUUID);
    if (action.type === taskActions.updateTaskCancelledTimeActions.success) {
        const currentValue = parent.taskGroup[action.data.taskUUID].time_cancelled;
        if (currentValue === null) {
            // only notify if marking rejected for the first time
            const restoreActions = yield () => [taskActions.updateTaskCancelledTimeRequest(
                action.data.taskUUID,
                {time_cancelled: null}
            )];
            const viewLink = `/task/${encodeUUID(action.data.taskUUID)}`
            yield put(displayInfoNotification("Task marked cancelled", restoreActions, viewLink))

这就是测试:

代码语言:javascript
复制
   it("updates the task state with cancelled time and sends a notification", () => {
        const action = {type: taskActions.updateTaskCancelledTimeActions.success, data: {taskUUID: "someUUID", time_cancelled: new Date().toISOString()}};
        const restoreActions = () => [taskActions.updateTaskCancelledTimeRequest(
            action.data.taskUUID,
            {time_cancelled: null}
        )];
        const viewLink = `/task/${encodeUUID(action.data.taskUUID)}`
        return expectSaga(testable.updateTaskTimeCancelledTimeRejectedSuccess, action)
            .provide([
                [select(getTasksSelector), {
                    tasksNew: {
                        1: {
                            someUUID: {
                                time_cancelled: null,
                                parent_id: 1
                            }
                        }
                    }
                }]])
            .put(displayInfoNotification("Task marked cancelled", restoreActions, viewLink))
            .run()


    })
})

这就是结果:

代码语言:javascript
复制
    SagaTestError:
    put expectation unmet:

    Expected
    --------
    { '@@redux-saga/IO': true,
      combinator: false,
      type: 'PUT',
      payload:
       { channel: undefined,
         action:
          { type: 'DISPLAY_INFO_NOTIFICATION',
            message: 'Task marked cancelled',
            restoreActions: [Function: restoreActions],
            viewLink: '/task/0000000000000000000000' } } }

    Actual:
    ------
    1. { '@@redux-saga/IO': true,
      combinator: false,
      type: 'PUT',
      payload:
       { channel: undefined,
         action:
          { type: 'DISPLAY_INFO_NOTIFICATION',
            message: 'Task marked cancelled',
            restoreActions: [Function],
            viewLink: '/task/0000000000000000000000' } } }

      at node_modules/redux-saga-test-plan/lib/expectSaga/expectations.js:48:13
      at node_modules/redux-saga-test-plan/lib/expectSaga/index.js:544:7
          at Array.forEach (<anonymous>)
      at checkExpectations (node_modules/redux-saga-test-plan/lib/expectSaga/index.js:543:18)

对我来说,很明显相等失败是因为restoreActions对象,它是一个不同引用的函数。我不知道该怎么做才能通过测试。我需要做什么来验证传奇是否如我预期的那样流畅?我不一定需要验证函数的结果,只需要验证是否发生了displayInfoNotification put。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-06 23:41:26

为您的restoreActions lambda创建一个工厂函数,然后您可以为您的测试模拟它。

例如:

代码语言:javascript
复制
// somewhere in your module
const actionRestoreFactory = (action) => () => [taskActions.updateTaskCancelledTimeRequest(action.data.taskUUID, {time_cancelled: null})]

//In your saga
yield put(displayInfoNotification("Task marked cancelled", actionRestoreFactory(action), viewLink))

//In your test
import actionRestoreFactory from './your/module'
jest.mock('./your/module')
actionRestoreFactory.mockReturnValue(jest.fn())
//...
return expectSaga(testable.updateTaskTimeCancelledTimeRejectedSuccess, action)
         .provide([ ... ])
         .put(displayInfoNotification("Task marked cancelled", actionRestoreFactory(action), viewLink))
         .run()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67403895

复制
相关文章

相似问题

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