我正在测试getArticlesFromDatabase 中的 axios 。
好像我做错了,导致控制台显示以下消息:
(节点:36919)UnhandledPromiseRejectionWarning:未处理的Promise拒绝(拒绝ID:5):这里是拒绝失败:(
节点:36919)DeprecationWarning:不推荐使用未处理的Promise拒绝。将来,未处理的Promise拒绝将使用非零退出代码终止 Node.js 进程。
如何解决?
csrfData.js
import axios from 'axios';
var getArticlesFromDatabase = new Promise(function(resolve, reject) {
axios.get('127.0.0.1:8000/api/articles/get-articles-list').then(response=>{
resolve('herer is resolve success: ',response.data);
}).catch(function (error) {
reject('herer is reject fail: ',error);
});
});
export {getArticlesFromDatabase};
csrfData.test.js
import React from 'react';
import {shallow, configure} from 'enzyme';
import Adapter from 'enzyme-adapter-react-15';
import {expect} from 'chai';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import {getArticlesFromDatabase} from '../components/common/csrfData';
configure({adapter: new Adapter()});
describe('csrfData', function () {
it('csrfData ', function () {
let mock = new MockAdapter(axios);
const data = { response: true };
mock.onGet('127.0.0.1:8000/api/articles/get-articles-list').reply(200, data);
getArticlesFromDatabase.then(function(value) {
console.log('getArticlesFromDatabase: ',value);
});
});
});