开玩笑的测试:无法读取未定义的属性“状态”

时间:2020-09-17 15:34:57

标签: javascript node.js express jestjs axis

我正在尝试使用Axios get请求测试功能。当我收到响应时,我正在调用另一个函数来过滤JSON文档。

// sample.js

import axios from "axios";

public async getFruitData{
  const response = await axios.get("https://api.url.com/search/fruit", {
    params: {
      client_id: process.env.REACT_APP_UNSPLASH_TOKEN,
      query: term
    }
  });

return this.filtersResponse(response.data.results); //<---throwing error
};

filterResponse(detailsResponse) {
  const excludeFruit = ['apple'];
  return detailsResponse.filter(record => !excludeFruit.includes(record.fruit_type));
}

这是我的笑话

import axios from 'axios';
import Fruit from '../app/fruit';

jest.mock('axios');

test('it filter jsont fruit  ', async () => {
  mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: 'test' }));
  const result = await getFruitData();
  expect(result).toBe({"data": "test"});
  expect(filterResponse(record)).toBeDefined()
});

运行测试时,它给了我以下错误-> TypeError: Cannot read property 'status' of undefined

如何测试这种情况

0 个答案:

没有答案