【问题标题】:How to mocking a http request with route params using axios-mock-adapter如何使用 axios-mock-adapter 模拟带有路由参数的 http 请求
【发布时间】:2018-04-07 20:49:54
【问题描述】:

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

【问题讨论】:

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


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-30
      • 2018-06-13
      • 1970-01-01
      • 2018-03-17
      • 2019-03-31
      • 2017-09-06
      • 1970-01-01
      • 2017-05-09
      相关资源
      最近更新 更多