如何使用路由参数模拟http请求?
var axios = require('axios');
var MockAdapter = require('axios-mock-adapter');
// This sets the mock adapter on the default instance
var mock = new MockAdapter(axios);
mock.onGet('/api/colleges/:collegeId/branches/:branchesId').reply(200);
最佳答案
将占位符转换为通配符并将路径转换为正则表达式:
function route (path = '') {
return typeof path === 'string'
? new RegExp(path.replace(/:\w+/g, '[^/]+'))
: path
}
像这样使用:
mock.onGet(route('/api/colleges/:collegeId/branches/:branchesId')).reply(200);
关于node.js - 如何使用 axios-mock-adapter 使用路由参数模拟 http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46954860/