我正在使用axios和jest-mock-axios。
当我测试我的API时,它显示此错误:
Expected mock function to have been called one time, but it was called zero times.
这是我的api文件:
import URI from '../../constants/uri';
import globalAxios from '../../utils/axiosConfig';
const api = {
checkStock: ({ products, vendorCode }) => {
const url = URI.BASKET.CHECK_STOCK({ products, vendorCode });
const config = {
url,
method: 'get',
};
return globalAxios(config);
},
};
export default api;
说明:
URI是一个对象,其中包含获取端点名称和输入参数并创建要请求的URL的函数。
globalAxios是一个获取方法,数据标头等的函数,并添加一些标头并使用给定的参数调用axios。
这是我的测试:
import mockAxios from 'jest-mock-axios';
import URI from '../../../constants/uri';
import api from '../api';
afterEach(() => {
mockAxios.reset();
});
const expectedData = {
products: [],
vendorCode: null,
};
describe('Basket api', () => {
describe('handle api request for checkStock', () => {
it('should request for checkStock api', () => {
api.checkStock(expectedData);
expect(mockAxios.get).toHaveBeenCalledTimes(1);
expect(mockAxios.get).toHaveBeenCalledWith(
URI.BASKET.CHECK_STOCK(expectedData),
);
});
});
});
如您所见,我打电话给
api.checkStock(expectedData);
在我的测试中,但运行时似乎没有调用API