翊君

V1

2022/05/18阅读:10主题:自定义主题1

Introductions to Mock Features

Introductions to Mock Features

The front-end development is often dependent on the back-end data interface, and it is often difficult to start work on the front-end until the back-end interface is ready.

The Mock feature is used to solve the problem. With the Mock tool, the front-end and back-end can be synchronized into development. Before the back-end interface is available, the front-end can use the Mock feature to create a fake data interface for development and debugging.

1. Feature Introductions

Based on the difinition of the interface or the data structure , Mock rule configuration and Mock expectation configuration, the Mock function can generate mock data automatically. And users can flexibly construct interfaces of various structures according to their requirements.

Generally, Apifox can generate highly user-friendly mock data with zero configuration.

  1. Apifox automatically generates mock rules according to the data structure and data type in the interface definition.

  2. Apifox has a built-in feature named Smart Mock, which optimizes the automatically generated mock rules according to the field name and data type of the field. For example, we have a field name whose data type is string. If the name contains the string 'image', an URL of the image address will be automatically mocked; If the name contains the string 'time', a time string will be automatically mocked; If the name contains the string 'city', a city name will be automatically mocked.

  3. According to the built-in rules (They can be turned off), Apifox can automatically recognize fields such as image, avatar, username, cell phone number, URL, date, time, timestamp, email, province, city, address, IP, etc., so as to mock out greatly user-friendly data.

  4. In addition to built-in mock rules, users can also customize the rule base to meet various personalized requirements. Wielding regular expressions and wildcard characters to match customized Mock rules for field names are supported.

The results generated by the Apifox zero-configuration Mock are shown as below:

2. Mock Request URL

Mock Request URL supports two ways as fllows:

  1. The interface path mode

http://127.0.0.1:4523/mock/{porjectID}/{interfacePath}

  1. The interface ID mode

http://127.0.0.1:4523/mock2/{projectID}/{interfaceID}

The request method should be consistent with the method in the interface definition.

For example, if your project ID is '18600', the mokced interface ID is '89343', the interface path is '/store/pets' and the request method is 'POST', so the actual Mock URL is like this:

POST http://127.0.0.1:4523/mock/18600/store/pets
or 
POST http://127.0.0.1:4523/mock2/18600/89343

By default, once the interface or data structure is defined, you can access the automatically mocked data interface via the URL above without any additional configuration.

Tips:

  1. The Mock service is started locally, so the ip address in the URL is 127.0.0.1. If other devices need to access mock data, just change 127.0.0.1 to the local intranet ip. If you still can't access it, please check if the port 4523 used by mock is restricted by the firewall.
  2. If there are too many interfaces in a project with the same 'method + path' path, you can only use interface ID mode, not interface path mode, otherwise path conflicts will happen.
  3. If the interface path does not start with /, you can only use the interface ID mode, not the interface path mode.
  4. The Mock service will be launched acquiescently without any other operation when you open Apifox.
  5. The front URL of the Mock service is fixed and cannot be modified. Changing the front URL in the environment named Mock Server does not change the actual front URL of the Mock service.

Get the Mock URL of the interface Open the 'Interface Details' - 'View' and check the Mock module of the page. Then you can acquire the Mock URLs corresponding to the interfaces.

3. Custom Mock rules

Apifox supports highly flexible customization of Mock rules to meet various business requirements.

3.1 Custom Mock rules of data structure

When defining data structures, you can set Mock rules manually, and taking advantage of data placeholder way to custom Mock rules is also supported. Please have a look at the Mock.js syntax.

3.2 The advanced settings of data fields

The Maximum, Minimum, Enumerated, Partten, and format values set in the advanced settings of the data field are also used as Mock rules.

3.3 The advanced Mock

The advanced mock is the most flexible way to implement Mock, enabling flexible and customized data structures (not limited by the data structure of the interface) and the ability to return different data depending on the value of the request parameters. See the Advanced Mock documentation.

3.4 Smart Mock

When the fields in the returned Response (or data model) of the interface are not configured with Mock rules, the system will automatically generate data using Smart Mock rules to achieve a user-friendly Mock with zero configuration. View the Smart Mock documentation

4. The priority of Mock rules

The order of priority of the Mock rules that are actually executed as the data fields are automatically mock data is shown as follows.

  1. Expectations set in the Interface Details - Advanced Mock (matched based on interface parameters).

  2. The Mock rule set in the field of the data structure.

  3. The maximum value, minimum value, enumeration value, partten, and format set in the advanced settings of the fields of the data structure.

  4. Project Settings - Customized rules set by Smart Mock.

  5. Project Settings - Built-in rules set by Smart Mock.

  6. The data type of the field in the data structure.

5. Other details

  1. By default, the system will mock the data structure of the first Response in the interface definition. If you want to mock other Responses, you can get the mock URL of other Responses in the Mock module of the 'Interface Details - View' page.

分类:

后端

标签:

后端

作者介绍

翊君
V1