【问题标题】:unit-test with HTTP request returns Promise { <pending> }带有 HTTP 请求的单元测试返回 Promise { <pending> }
【发布时间】:2019-06-30 07:37:25
【问题描述】:

我正在使用 axios 模拟适配器来模拟 HTTP 请求以测试我的功能。在我为函数定义了行为之后,然后我创建了一个类的实例来调用函数,结果是

**Promise { <pending> }**, 

有什么问题?如何返回我定义的值?

这是我的代码:

UserService.js

export default class UserService {
  getUserInfo = userId => {
    const params = {
      userId,
    };

    return axios
      .get('https://www.usefortesting.com', {
        params: { userId: params },
      })
      .then(response => response.data.userInfo)
      .catch(error => error);
  };
}

UserService.test.js

import React from 'react';
import axios from 'axios';
import UserService from './UserService';
import MockAdapter from 'axios-mock-adapter';

describe('testing', () => {
  let axiosMock;
  const Info = {
    userInfo: {
      id: '123',
      name: 'omg',
    },
  };
  beforeEach(function() {
    axiosMock = new MockAdapter(axios);
  });

  afterEach(() => {
    axiosMock.reset();
    axiosMock.restore();
  });

  it('testing', () => {
    axiosMock
      .onGet('https://www.usefortesting.com', {
        params: { userId: 'user_1' },
      })
      .reply(200, Info);
    let userService = new UserService();
    let response = userService.getUserInfo('user_1');
    console.log(response);
  });
});

【问题讨论】:

    标签: javascript node.js reactjs unit-testing axios


    【解决方案1】:

    您需要在测试中等待响应。如下所示,使用回调或 async/await。
    你的测试应该是这样的:

    it('testing', async () => {  // notice async here
      axiosMock
        .onGet('https://www.usefortesting.com', {
          params: { userId: 'user_1' },
        })
        .reply(200, Info);
      let userService = new UserService();
      let response = await userService.getUserInfo('user_1');  // notice await here
      console.log(response);
    });
    

    it('testing', () => {
      ...
      userService.getUserInfo('user_1').then(response => {
        console.log(response);
      });
    });
    

    您可以在 jest 文档上查看 this link 以获取更多示例。


    您的getUserInfo() 方法也有错误,在参数中您正在为userId 传递一个对象,但您需要传递字符串或整数。你应该做的是:

    return axios.get('https://www.usefortesting.com', {
        params: { userId: params.userId },
    })...
    

    return axios.get('https://www.usefortesting.com', {
        params,
    })...
    

    【讨论】:

    • 嗨,Vaibhav,我使用了你提供的方法,然后返回如下内容: response: { status: 404, config: { transformRequest: [Object], transformResponse: [Object], timeout: 0, xsrfCookieName: 'XSRF-TOKEN', xsrfHeaderName: 'X-XSRF-TOKEN', maxContentLength: -1, validateStatus: [Function: validateStatus], headers: [Object], method: 'get', params: [Object] , url: 'usefortesting.com', data: undefined }, data: undefined }, 这里的数据还是 undefined
    • UserServicegetUserInfo 方法中的参数不正确。再次查看您传递的参数。你应该做的是:return axios.get('https://www.usefortesting.com', { params: params }) 或只是return axios.get('https://www.usefortesting.com', { params })
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 2023-04-01
    相关资源
    最近更新 更多