我如何在开玩笑中测试axios


78

我有这个反应

export function fetchPosts() {
    const request = axios.get(`${WORDPRESS_URL}`);
    return {
        type: FETCH_POSTS,
        payload: request
    }
}

在这种情况下,如何测试axios?开玩笑说,在那个网站上有一个异步代码的用例,他们使用了一个模拟函数,但是我不知道我是否可以用axios做到这一点?参考:https : //facebook.github.io/jest/docs/tutorial-async.html

到目前为止,我已经测试了它是否返回了正确的类型

it('should dispatch actions with the correct type', () => {
    store.dispatch(fetchPosts());
    let action = store.getActions();
    expect(action[0].type).toBe(FETCH_POSTS);
});

我不知道如何传递模拟数据并测试它是否返回,但是有人有任何想法吗?

先感谢您


Answers:


60

我使用了axios-mock-adapter。在这种情况下,服务在./chatbot中描述。在模拟适配器中,您指定使用API​​端点时返回的内容。

import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import chatbot from './chatbot';

describe('Chatbot', () => {
    it('returns data when sendMessage is called', done => {
        var mock = new MockAdapter(axios);
        const data = { response: true };
        mock.onGet('https://us-central1-hutoma-backend.cloudfunctions.net/chat').reply(200, data);

        chatbot.sendMessage(0, 'any').then(response => {
            expect(response).toEqual(data);
            done();
        });
    });
});

您可以在此处查看整个示例:

服务: https //github.com/lnolazco/hutoma-test/blob/master/src/services/chatbot.js

测试:https : //github.com/lnolazco/hutoma-test/blob/master/src/services/chatbot.test.js


108

不使用任何其他库:

import * as axios from "axios";

// Mock out all top level functions, such as get, put, delete and post:
jest.mock("axios");

// ...

test("good response", () => {
  axios.get.mockImplementation(() => Promise.resolve({ data: {...} }));
  // ...
});

test("bad response", () => {
  axios.get.mockImplementation(() => Promise.reject({ ... }));
  // ...
});

可以指定响应代码:

axios.get.mockImplementation(() => Promise.resolve({ status: 200, data: {...} }));

可以根据参数更改模拟:

axios.get.mockImplementation((url) => {
    if (url === 'www.example.com') {
        return Promise.resolve({ data: {...} });
    } else {
        //...
    }
});

Jest v23引入了一些语法糖来嘲笑Promises:

axios.get.mockImplementation(() => Promise.resolve({ data: {...} }));

可以简化为

axios.get.mockResolvedValue({ data: {...} });

被拒绝的承诺也有一个等效项:mockRejectedValue

进一步阅读:


28

我可以按照以下步骤进行操作:

  1. 创建一个文件夹__mocks __ /(由@Januartha评论指出)
  2. 实现一个axios.js模拟文件
  3. 测试中使用我实现的模块

模拟将自动发生

模拟模块的示例:

module.exports = {
    get: jest.fn((url) => {
        if (url === '/something') {
            return Promise.resolve({
                data: 'data'
            });
        }
    }),
    post: jest.fn((url) => {
        if (url === '/something') {
            return Promise.resolve({
                data: 'data'
            });
        }
        if (url === '/something2') {
            return Promise.resolve({
                data: 'data2'
            });
        }
    }),
    create: jest.fn(function () {
        return this;
    })
};

1
嗨@ shorif2000,我在这里实现了它github.com/vspedr/movile-messaging/pull/8/files
Cavalcante Filho,

1
里面写手动嘲笑嘲笑为是不是一个好的做法手册嘲笑文件说要编写内部手册嘲笑__mocks__
Januartha

@Januartha对不起,我打错了。我要在这里更正它。我在响应中输入了“ ____mock___”,它的意思是粗体。抱歉
Amadeu Cavalcante Filho

@AmadeuCavalcanteFilho啊,没问题,:)
Januartha

3

我已经用nock做到了,就像这样:

import nock from 'nock'
import axios from 'axios'
import httpAdapter from 'axios/lib/adapters/http'

axios.defaults.adapter = httpAdapter

describe('foo', () => {
    it('bar', () => {
        nock('https://example.com:443')
            .get('/example')
            .reply(200, 'some payload')

        // test...
    })
})

1
我确实尝试过此方法,但是我似乎axios不能与nock一起玩。github.com/node-nock/nock/issues/699,但仍然感谢您的帮助
将于

0

对于那些希望使用axios-mock-adapter代替redux文档中的嘲讽示例进行异步测试的人,我成功地利用了以下内容

actions.test.js

describe('SignInUser', () => {
  var history = {
    push: function(str) {
        expect(str).toEqual('/feed');
    }
  }

  it('Dispatches authorization', () => {
    let mock = new MockAdapter(axios);
    mock.onPost(`${ROOT_URL}/auth/signin`, { 
        email: 'test@test.com', 
        password: 'test'
    }).reply(200, {token: 'testToken' });

    const expectedActions = [ { type: types.AUTH_USER } ];
    const store = mockStore({ auth: [] });

    return store.dispatch(actions.signInUser({ 
      email: 'test@test.com', 
      password: 'test',
    }, history)).then(() => {
        expect(store.getActions()).toEqual(expectedActions);
  });

});

为了测试一个成功的案例signInUseractions/index.js

export const signInUser = ({ email, password }, history) => async dispatch => {
  const res = await axios.post(`${ROOT_URL}/auth/signin`, { email, password })
    .catch(({ response: { data } }) => {
        ...
  });

  if (res) {
    dispatch({ type: AUTH_USER });                 // test verified this
    localStorage.setItem('token', res.data.token); // test mocked this
    history.push('/feed');                         // test mocked this
  }
}

鉴于这是在开玩笑,所以必须模拟localstorage调用。这是在src/setupTests.js

const localStorageMock = {
  removeItem: jest.fn(),
  getItem: jest.fn(),
  setItem: jest.fn(),
  clear: jest.fn()
};
global.localStorage = localStorageMock;

您如何使用localStorageMock
Kermit_ice_tea

@Kermit_ice_teasrc/setupTests.js被记录在create-react-app中,作为笑话/酶测试的全局设置。在该文件中,我创建了一个localStorageMock用虚拟函数(getItem,setItem)任意调用的对象。魔力位于我设置global.localStorage等于该对象的底部。我可以在一行中完成此操作,而不用命名对象localStorageMock。整个设置的目的是简单地避免在测试期间破坏本地存储的任何代码被破坏。
vapurrmaid
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.