4

如何使用路由参数模拟 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);
4

1 回答 1

1

将占位符转换为通配符并将路径转换为正则表达式:

function route (path = '') {
  return typeof path === 'string'
    ? new RegExp(path.replace(/:\w+/g, '[^/]+'))
    : path
}

像这样使用:

mock.onGet(route('/api/colleges/:collegeId/branches/:branchesId')).reply(200);
于 2017-12-08T02:30:29.960 回答