This is the third day of my participation in the August More Text Challenge
The mock. Js is introduced
The Mock. Js’s official website
Mock. Js is a mock data generator designed to help front-end engineers create their own data without having to chase their friends from the back end. Does that mean the front end doesn’t need the back end? Of course not. Mock. Js just provides us with some data that the back end is going to transfer to us, which is actually simulated data for our front-end testing. In normal development, we sometimes have to wait for the back-end interface, which is relatively passive. If the requirements are not complex, we can manually simulate some background data, but this method has great limitations. If the amount of data is large and the interaction is complex, it is too inefficient. At this point you can use mock. Js to simulate the background data you need to solve the problem easily. Mock. js has two main functions. The first is that we can generate rich mock data based on the data template. The second feature is that it intercepts Ajax requests. Mock. js can intercept Ajax requests and return mock response data without modifying existing code.
Mock methods intercept Ajax requests
Mock. js has two main functions. The first is that we can generate rich mock data based on the data template. The second feature is that it intercepts Ajax requests. Earlier we saw how to generate rich simulation data from a template of data. Let’s take a look at how it blocks Ajax requests. Mock. Mock () can be used to block Ajax requests, such as a JSON file requested in your project, to create a JSON file, and then request its address via Ajax. Mock. Mock () can be used to block Ajax requests, and Mock methods will return data to you. The data returned is the data generated by the data template or placeholder or method in the Random object we talked about earlier. Let’s look at mock methods. Mock.mock( rurl? , rtype? , the template | function (options)) the meaning of the parameters and the default values as shown below
- Parameter rurl: optional. Indicates the URL to be intercepted. It can be a URL string or A URL re. For example,
//domain/list.json/
,'/domian/list.json'
. - Parameter rtype: optional. Represents the type of Ajax request to intercept. For example,
GET
,POST
,PUT
,DELETE
And so on. - Parameters of the template: optional. Represents a data template, which can be an object or a string. For example,
{ 'data|1-10':[{}] }
,'@EMAIL'
. - Parameter function(options) : Optional. Represents the function used to generate the response data.
- Options: Points to the set of Ajax options for this request.
The use of different arguments to mock methods
-
Mock.mock( template )
Generate simulated data from the data template.
-
Mock.mock( rurl, template )
Record the data template. When an Ajax request matching an RURL is intercepted, mock data is generated from the data template and returned as response data.
-
Mock.mock( rurl, function(options) )
Records the function used to generate the response data. When an Ajax request matching an RURL is intercepted, the function(options) is executed and the result is returned as response data.
-
Mock.mock( rurl, rtype, template )
Record the data template. When Ajax requests matching rURL and RType are intercepted, mock data is generated from the data template and returned as response data.
-
Mock.mock( rurl, rtype, function(options) )
Records the function used to generate the response data. When an Ajax request matching rURL and RType is intercepted, the function(options) is executed and the result is returned as response data.