# API Mock
In order to make it easier for developers to develop Weixin Mini Program, the developer tools provide the ability to API Mock, which can simulate the results of some API calls.
# Operating environment
- Download and install the developer tools
1.02.2003062or above, and download the download for .
# How to use it
# Basic usage
The entry of the mock Tab at the top of the tool debugging panel, click + New Rule

Examples of Rules

Run the following code:
wx.request({
url: 'https://example.com/ajax?dataType=member',
dataType: 'json',
success(res) {
console.log(res)
}
})
You will hit the above rule and get the following data:

# Rule priorities
When multiple rules are configured for an API, the rules ahead of will match first, and when one rule is satisfied, the following rule will not be executed again.
You can change the priority of the rules by dragging and dropping them to get the desired mock results.
# Multiple levels of parameter matching
If the matching parameter type is not a primitive type, but an attribute under an Object parameter, a multi-level match is needed.
For example: When thewx.requestcall thedataparameter with aWhenmockhas an attribute value oftrue, the result is only returned through the Mock rule. The parameter matching rule should be filled out as follows:

Thewx.requestcall in the above example needs to be modified to:
wx.request({
url: 'https://example.com/ajax?dataType=member',
data: { // \
mock: true // - 添加这三行
}, // /
dataType: 'json',
success(res) {
console.log(res)
}
})
Only then can we meet this rule.
# Using data templates
When the data that needs to be simulated is complex, you can use a data template to quickly generate data that meets your requirements.
The syntax of the data template is from the open source library Mock.js , and the syntax can be viewed in detail in the project documentation.
For example, in the above example, you want to return a list containing 10 different data items, you can select "Data Template" in "Data Generation" and fill in the following template:
{
"data": {
"list|10": [
{"id|+1": 1, "name": "@FIRST"}
]
},
"statusCode": 200,
"header": {
"content-type": "application/json; charset=utf-8"
}
}
Below the Template Editor, a data preview is generated in real time based on the template to make it easy to confirm the correctness of the template.

Recalling the above code will return:

# Rules Import and Export
When you need to share rules with other members of the project, you can export the rule configuration in Mock, and then send the configuration file to other members to import it.
# Note
- Regular expressions in rule matching do not distinguish between uppercase and lowercase;
Temporarily unable to match withdataTypeparameter in wx.request;- "." is a special character in the argument name in the match rule, and will not match if the argument name to be matched contains., for example
{ "propName": { "key": "value", // 通过 propName.namespace1.key 无法匹配到此参数,应尽量避免这种情况。 "namespace1.key": "value1" } } - 蓝牙模块相关的 API,实际调用中需要先调用
wx.openBluetoothAdapter进行初始化,但是在 Mock 的规则命中后将没有这些限制,需要注意- getBLEDeviceServices
- getBLEDeviceCharacteristics
- createBLEConnection
- closeBLEConnection
- writeBLECharacteristicValue
- startBluetoothDevicesDiscovery
- stopBluetoothDevicesDiscovery
- getConnectedBluetoothDevices
- getBluetoothDevices
- getBluetoothAdapterState
- closeBluetoothAdapter
- 目前支持Mock 的 API 列表:
- request
- downloadFile
- getLocation
- checkSession
- requestPayment
- startSoterAuthentication
- checkIsSupportSoterAuthentication
- checkIsSoterEnrolledInDevice
- startBeaconDiscovery
- stopBeaconDiscovery
- getBeacons
- startWifi
- stopWifi
- getConnectedWifi
- connectWifi
- getBLEDeviceServices
- getBLEDeviceCharacteristics
- createBLEConnection
- closeBLEConnection
- writeBLECharacteristicValue
- startBluetoothDevicesDiscovery
- stopBluetoothDevicesDiscovery
- openBluetoothAdapter
- getConnectedBluetoothDevices
- getBluetoothDevices
- getBluetoothAdapterState
- closeBluetoothAdapter
- stopHCE
- startHCE
- sendHCEMessage
- getHCEState
- setBackgroundFetchToken
- getBackgroundFetchToken
- getBackgroundFetchData