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

如何Mock Axios调用?Student组件测试遇两类错误求解决方案

问题解决方案

错误2:TypeError: Cannot read properties of undefined (reading subjectName)

这个错误源于两个问题:

  1. 变量名误用:渲染时错误调用了状态更新函数setStudentSubjects[0],实际应该使用状态变量studentSubjects[0]
  2. 未处理异步加载的空状态:studentSubjects初始值为null,在axios请求完成前直接访问[0].subjectName会触发报错,必须添加空值校验。

修正后的组件渲染部分:

return (
  <div>
    <div>{studentRecord.studentNumber}</div>
    {/* 先校验数据是否存在再渲染 */}
    {studentSubjects?.length > 0 && (
      <div>{studentSubjects[0].subjectName}</div>
    )}
    {/* 可选:添加加载状态提示 */}
    {!studentSubjects && <div>加载中...</div>}
  </div>
);

额外优化:studentRecord直接来自props,若不需要在组件内部修改,无需额外声明为状态,直接用props.stRecord即可,避免不必要的状态同步。


错误1:组件更新未被包裹在act(...)中

该问题是因为测试未等待异步请求完成后的组件状态更新。React测试要求所有状态更新需在act上下文执行,使用@testing-library/react的waitFor可自动处理此逻辑,同时需调整axios的mock方式:

  1. 在测试文件顶部mock axios模块:
jest.mock('axios');
  1. 修正测试逻辑,添加waitFor等待异步更新完成:

修正后的测试代码:

import { render, waitFor } from '@testing-library/react';
import axios from 'axios';
import { StudentSubjectsData } from "../globalDataProvider";
import Student from './Student'; // 替换为你的组件实际路径

// 假设测试用的StudentRecord已定义
const StudentRecord = { studentNumber: 'S1001' };

it("make sure student renders", async () => {
  // mock axios.get的返回数据
  axios.get.mockResolvedValue({ data: StudentSubjectsData() });
  
  const { getByText } = render(<Student stRecord={StudentRecord}/>);
  
  // 等待异步数据加载完成,验证目标内容渲染
  await waitFor(() => {
    expect(getByText(StudentSubjectsData()[0].subjectName)).toBeInTheDocument();
  });
});

原测试中await Promise.resolve(...)属于冗余代码,直接使用mockResolvedValue即可。


额外优化建议

  • 将studentSubjects初始值设为空数组[]而非null,简化空值校验逻辑:
const [studentSubjects, setStudentSubjects] = useState([]);
  • 给axios请求添加错误处理,避免请求失败时组件一直处于异常状态:
function getStudentSubjects() {
  const apicalladdress = `/studentapi/GetStudentSubjects/${studentRecord.studentNumber}`;
  axios.get(apicalladdress)
    .then((result) => {
      setStudentSubjects(result.data);
    })
    .catch((error) => {
      console.error('加载科目数据失败:', error);
      setStudentSubjects([]); // 或设置错误状态提示用户
    });
}

内容的提问来源于stack exchange,提问作者V R

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 10:46:19