如何使用axios-mock-adapter模拟带有路径参数的http请求

时间:2017-10-26 12:51:37

标签: node.js express axios axios-mock-adapter

如何使用route params模拟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);

1 个答案:

答案 0 :(得分:0)

将占位符转换为通配符和RegExp的路径:

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

像这样使用:

mock.onGet(route('/api/colleges/:collegeId/branches/:branchesId')).reply(200);