You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Redux-saga中如何覆盖变量声明代码行?测试用例编写疑问

Redux-Saga测试:覆盖变量声明行的解决方案

针对你在编写Redux-Saga测试时,无法覆盖var url = "v1/globalSearch/globalSearchTextForAll?pageSize="+action.pageSize+"&recordFromIndex="+action.recordFromIndex这类变量声明代码行的问题,以下是具体的测试方案:

核心思路

要覆盖变量声明行,只需让saga执行到该代码逻辑即可。借助redux-saga/test-utils提供的expectSaga工具,构造符合要求的action并模拟依赖服务的返回,就能触发saga的完整执行流,包括变量声明、条件分支和异常处理。

具体测试代码示例

import { expectSaga } from 'redux-saga/test-utils';
import { globalSearchAllAsync } from './your-saga-file'; // 替换为你的saga文件路径
import NoccSearchServiceAxios from './your-service-file'; // 替换为你的服务文件路径
import { globalSearchAllSuccess, fetchFailure } from './your-action-file'; // 替换为你的action文件路径

describe('globalSearchAllAsync saga', () => {
  // 基础用例:覆盖url初始声明行
  it('执行基础搜索逻辑,覆盖url声明', () => {
    const mockAction = {
      type: 'GLOBAL_SEARCH_ALL_ASYNC',
      pageSize: 10,
      recordFromIndex: 0,
      requestObj: { keyword: 'test' }
    };

    const mockResponse = {
      data: {
        response: { list: [] }
      }
    };

    // 模拟axios post请求的返回结果
    jest.spyOn(NoccSearchServiceAxios, 'post').mockResolvedValue(mockResponse);

    return expectSaga(globalSearchAllAsync, mockAction)
      .put(globalSearchAllSuccess(mockResponse.data.response))
      .run();
  });

  // 分支用例:覆盖sortBy参数拼接逻辑
  it('传入sortBy参数时,拼接url并执行搜索', () => {
    const mockAction = {
      type: 'GLOBAL_SEARCH_ALL_ASYNC',
      pageSize: 10,
      recordFromIndex: 0,
      sortBy: 'createTime',
      requestObj: { keyword: 'test' }
    };

    const mockResponse = {
      data: {
        response: { list: [] }
      }
    };

    jest.spyOn(NoccSearchServiceAxios, 'post').mockResolvedValue(mockResponse);

    return expectSaga(globalSearchAllAsync, mockAction)
      .put(globalSearchAllSuccess(mockResponse.data.response))
      .run();
  });

  // 分支用例:覆盖sortOrder参数拼接逻辑
  it('传入sortOrder参数时,拼接url并执行搜索', () => {
    const mockAction = {
      type: 'GLOBAL_SEARCH_ALL_ASYNC',
      pageSize: 10,
      recordFromIndex: 0,
      sortOrder: 'desc',
      requestObj: { keyword: 'test' }
    };

    const mockResponse = {
      data: {
        response: { list: [] }
      }
    };

    jest.spyOn(NoccSearchServiceAxios, 'post').mockResolvedValue(mockResponse);

    return expectSaga(globalSearchAllAsync, mockAction)
      .put(globalSearchAllSuccess(mockResponse.data.response))
      .run();
  });

  // 异常用例:覆盖catch分支逻辑
  it('请求失败时触发错误action', () => {
    const mockAction = {
      type: 'GLOBAL_SEARCH_ALL_ASYNC',
      pageSize: 10,
      recordFromIndex: 0,
      requestObj: { keyword: 'test' }
    };

    const mockError = {
      response: {
        data: {
          errorResponse: {
            errorMessage: '搜索失败,请重试'
          }
        }
      }
    };

    jest.spyOn(NoccSearchServiceAxios, 'post').mockRejectedValue(mockError);

    return expectSaga(globalSearchAllAsync, mockAction)
      .put(fetchFailure(mockError.response.data.errorResponse.errorMessage, mockAction.type))
      .run();
  });
});

说明

  • 上述测试用例覆盖了saga中的所有代码路径:包括url的初始声明、两个条件分支(sortBy/sortOrder)、正常请求分支和异常分支。
  • 运行测试后,查看jest覆盖率报告,就能看到变量声明行已被标记为覆盖状态。
  • 需根据实际项目路径替换示例中的文件路径、action类型和mock数据。

内容的提问来源于stack exchange,提问作者Jeevanandham K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 08:16:02