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

responseFake中的写法疑问 #4244

Open
samXxts opened this issue Jun 20, 2024 · 0 comments
Open

responseFake中的写法疑问 #4244

samXxts opened this issue Jun 20, 2024 · 0 comments

Comments

@samXxts
Copy link

samXxts commented Jun 20, 2024

Question(提问)

您好,以下疑问希望解答:
function registerRoutes(app) {
let mockLastIndex
const { mocks } = require('./index.js')
const mocksForServer = mocks.map(route => {
return responseFake(route.url, route.type, route.response)
})
for (const mock of mocksForServer) {
app[mock.type](mock.url, mock.response)
mockLastIndex = app._router.stack.length
}
const mockRoutesLength = Object.keys(mocksForServer).length
return {
mockRoutesLength: mockRoutesLength,
mockStartIndex: mockLastIndex - mockRoutesLength
}
}

// user login
{
url: '/vue-element-admin/user/login',
type: 'post',
response: config => { // config 用于模拟请求的配置 他不是后面我们看到的req和res
console.log('config', config)
console.log('config.body', config.body)
const { username } = config.body
const token = tokens[username]

  // mock error
  if (!token) {
    return {
      code: 60204,
      message: 'Account and password are incorrect.'
    }
  } 

  return {
    code: 20000,
    data: token
  }
}

},

Steps to reproduce(问题复现步骤)

  1. res.json(Mock.mock(respond instanceof Function ? respond(req, res) : respond))
    在执行respond(req, res)函数时 为什么没有报错
  2. 难道是因为mock框架的原因 对req res config这三者之间产生了关联吗

Other relevant information(格外信息)

  • Your OS:
  • Node.js version:
  • vue-element-admin version:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant