Ade*_*ear 53 reactjs jestjs react-redux
我有这个行动的反应
export function fetchPosts() {
const request = axios.get(`${WORDPRESS_URL}`);
return {
type: FETCH_POSTS,
payload: request
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下如何测试axios?Jest在那里有这个用例的异步代码,他们使用模拟函数,但我不知道我是否可以用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);
});
Run Code Online (Sandbox Code Playgroud)
我不知道如何传递模拟数据并测试它返回但是,有没有人有任何想法?
先感谢您
A J*_*lay 53
不使用任何其他库:
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({ ... }));
// ...
});
Run Code Online (Sandbox Code Playgroud)
可以指定响应代码:
axios.get.mockImplementation(() => Promise.resolve({ status: 200, data: {...} }));
Run Code Online (Sandbox Code Playgroud)
可以根据参数更改模拟:
axios.get.mockImplementation((url) => {
if (url === 'www.example.com') {
return Promise.resolve({ data: {...} });
} else {
//...
}
});
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅Jest模拟文档.
小智 47
我使用了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();
});
});
});
Run Code Online (Sandbox Code Playgroud)
你可以在这里看到整个例子:
服务: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
Ama*_*lho 21
我可以按照以下步骤做到这一点:
axios.js模拟文件模拟会自动发生
模拟模块的示例:
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;
})
};
Run Code Online (Sandbox Code Playgroud)
STR*_*NEY 10
看这个
album.jsconst fetchAlbum = function () {
return axios
.get("https://jsonplaceholder.typicode.com/albums/2")
.then((response) => {
return response.data;
});
};
Run Code Online (Sandbox Code Playgroud)
album.test.jsconst axios = require("axios");
const { fetchAlbum } = require("../utils.js");
jest.mock("axios");
test("mock axios get function", async () => {
expect.assertions(1);
const album = {
userId: 1,
id: 2,
title: "sunt qui excepturi placeat culpa",
};
const payload = { data: album };
// Now mock axios get method
axios.get = jest.fn().mockResolvedValue(payload);
await expect(fetchAlbum()).resolves.toEqual(album);
});
Run Code Online (Sandbox Code Playgroud)
我已经用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...
})
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57143 次 |
| 最近记录: |