Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve http mock #101

Closed
sorrycc opened this issue Feb 6, 2018 · 5 comments
Closed

Improve http mock #101

sorrycc opened this issue Feb 6, 2018 · 5 comments

Comments

@sorrycc
Copy link
Member

sorrycc commented Feb 6, 2018

  • 没有配置文件,约定自动读 ./mock/**/*.js
@xiaohuoni
Copy link
Member

不是很理解这个约定。
我在./mock/user.js里面写

const  apiPrefix  = 'oni';
module.exports = {
  [`GET ${apiPrefix}/user/logout`] (req, res) {
    res.status(200).end()
  },
}

然后,url地址应该请求什么?
尝试了
http://localhost:8001/oni/user
http://localhost:8001/user/oni/user
http://localhost:8001/mock/user/oni/user
都不正确。

@xiaohuoni
Copy link
Member

@xiaohuoni
Copy link
Member

@sorrycc

@sorrycc
Copy link
Member Author

sorrycc commented Feb 19, 2018

这个还没做,你需要在 .umirc.mock.js 里写,参考 https://github.com/umijs/umi/blob/master/examples/func-test/.umirc.mock.js

@minwe
Copy link
Contributor

minwe commented Mar 23, 2018

自己加了个加载 mock 目录的逻辑:

// .roadhogrc.mock.js
const fs = require('fs');
const path = require('path');

const mockAPI = {};

fs.readdirSync(path.join(__dirname, 'mock')).forEach(file => {
  const isFile = fs.statSync(path.join(__dirname, 'mock', file)).isFile();
  if (isFile) {
    const extname = path.extname(file);

    switch (extname) {
      case '.js':
        // eslint-disable-next-line
        Object.assign(mockAPI, require(`./mock/${file}`));
        break;
      case '.json':
        Object.assign(mockAPI, {
          [file]: file,
        });
        break;
      default:
    }
  }
});

module.exports = mockAPI;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants