微信小程序 - 模拟数据工具
wechatide://minicode/wCHxXXmf6OYz
-
- copy文件 https://raw.githubusercontent.com/webx32/WxMock/1.0.1-beta3/dist/mock.js (dist/mock.js此文件小程序不支持) dist/WxMock.js 到小程序工程根目录的 utils目录下
-
- 在app.js 引入 WxMock 代码
var Mock = require("./utils/WxMock.js");
-
- 在app.js中书写需要模拟的接口及返回结构
Mock.mock('https://xxx.com/users',{
"code":200,
"data|1-20":[
{
"name":function() {
return Mock.Random.cname()
},
"lastLogin":function() {
return Mock.Random.datetime()
}
}
]
})
Mock.mock('https://xxx.com/user/delete',{
"code":200,
"message":"s删除成功"
})
-
- 只要在 wx.request 中使用url为 mock对应的地址 就会返回响应mock数据
wx.request({
url: 'https://xxx.com/users',
success:function(res){
console.log('https://xxx.com/users',res);
}
})
Mock.js 使用方式见 http://mockjs.com/examples.html
Mock.js is a simulation data generator to help the front-end to develop and prototype separate from the back-end progress and reduce some monotony particularly while writing automated tests.
The official site: http://mockjs.com
- Generate simulated data according to the data template
- Provide request/response mocking for ajax requests with jQuery and KISSY
- Generate simulated data according to HTML-based templates
If you have any questions, please feel free to ask through New Issue.
Make sure the problem you're addressing is reproducible. Use http://jsbin.com/ or http://jsfiddle.net/ to provide a test page. Indicate what browsers the issue can be reproduced in. What version of Mock.js is the issue reproducible in. Is it reproducible after updating to the latest version?
Mock.js is available under the terms of the MIT License.