【问题标题】:jest tests for Axios.all and Axios.spreadAxios.all 和 Axios.spread 的笑话测试
【发布时间】:2019-09-30 02:03:31
【问题描述】:

我正在努力为以下方法编写 JEST 测试用例

getStudentList (studentList:}[]) {

    if (studentList.length < 1) {
      Promise.resolve()
    }
    let promises = []
    for (const student of StudentList) {
      if (!student.name) {
        Promise.resolve()
      }
      var url = `${API_URL}/${student.name}/`

      promises.push(Axios.get(url}))
    }

    return Axios.all(promises)
      .then(Axios.spread((...args) => {
        // customise the response here
        return args
          .map(response => response.data)
          .map(data => {
            //do something with data
            return data
          })
      }))

它使用 axios.all 和 axios.spread 来取回数据。我已经为 Axios.get 编写了简单的测试用例。但是如何为此编写测试用例?这个方法在一个vue项目中的一个服务类中

【问题讨论】:

  • 请注意,您的实现存在很多问题,例如你的参数的默认值,Promise.resolve() 什么都不做,StudentList 没有定义,Axios.spread 有点没用,因为你使用的是 rest 运算符
  • 你能详细说明一下其余操作符吗?
  • 如何改进promise.resolve?如果学生列表为空,我只是不想做任何 axios 调用..
  • 在for循环中,你应该使用continue;或const students = studentList.filter(student =&gt; student.name)
  • 您能否发布一个单独的问题来改进Axios.spread 的代码?

标签: vue.js jestjs axios


【解决方案1】:

这是一个简短示例,说明如何为上面的代码编写期望(覆盖率 100%):

import myService from './myService';
import Axios from 'axios';

jest.mock('axios');

global.API_URL = 'http://example.com/mock_api';

describe('myService', () => {
  describe('getStudentList', () => {
    describe('without students in the list', () => {
      it('should result undefined', () => {
        const result = myService.getStudentList();
        expect(result).resolves.toEqual( undefined );
      });
    });

    describe('with students in the list', () => {
      const mockStudentList = [{
        name: 'student1',
      }, {
        someProp: 'some value',
      }, {
        name: 'student3',
      }];

      const results = [];
      const mockAxiosSpreadResult = jest.fn();

      beforeAll(() => {
        Axios.get.mockClear();
        Axios.all.mockResolvedValue(results);
        Axios.spread.mockReturnValue(mockAxiosSpreadResult);
        myService.getStudentList( mockStudentList );
      });

      it('should call Axios.get once for each student with name', () => {
        expect(Axios.get).toHaveBeenCalledWith(`${API_URL}/student1/`);
        expect(Axios.get).toHaveBeenCalledWith(`${API_URL}/student3/`);
      });

      it('should call Axios.spread with a callback', () => {
        expect(Axios.spread).toHaveBeenCalledWith(expect.any(Function));
      });

      it('should call the result of Axios.spread with the resolved value of Axios.all', () => {
        expect(mockAxiosSpreadResult).toHaveBeenCalledWith(results);
      });

      describe('Axios.spread callback', () => {
        let callback;
        beforeAll(() => {
          callback = Axios.spread.mock.calls[0][0];
        });

        describe('called with parameters', () => {
          let result;
          beforeAll(() => {
            result = callback({
              data: 1
            },{
              data: 2
            },{
              data: 3
            },{
              data: 4
            });
          });

          it('should do something with the data', () => {
            expect(result).toEqual([1,2,3,4]);
          });
        });
      });
    });
  });
});

working example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 2018-08-09
    • 2019-09-06
    • 2019-07-05
    • 2020-12-15
    • 2019-08-17
    • 2021-06-13
    相关资源
    最近更新 更多