我正在尝试在 React 中测试我的 axios API 函数。
在这里发现了这个问题:我如何测试 axios in jest这指向使用axios-mock-adapter
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();
});
});
});
真正的功能:
/**
* Retrieve all Akamai images
* @param {String} akamai Akamai url
* @return {Thenable} Resolved: Akamai images
*/
export const callGetAkamai = () =>
makeRequest('/akamai', 'GET')
.catch(defaultCatch('callGetAkamai'));
我的测试:
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { callGetAkamai } from './api';
describe('GetAkamai', () => {
it('returns data when callGetAkamai is called', (done) => {
console.log('MockAdapter', MockAdapter);
const mock = new MockAdapter(axios);
// const mock = axios.create({
// baseURL: 'https://us-central1-hutoma-backend.cloudfunctions.net/chat/'
// });
const data = { response: true };
mock.onGet('https://us-central1-hutoma-backend.cloudfunctions.net/chat').reply(200, data);
callGetAkamai().then((response) => {
expect(response).toEqual(data);
done();
});
});
});