Apidog

All-in-one Collaborative API Development Platform

API Design

API Documentation

API Debugging

API Mock

API Automated Testing

Sign up for free

A Beginner's Tutorial on Mock Syntax

Start for free
Contents
Home / / A Beginner's Tutorial on Mock Syntax

A Beginner's Tutorial on Mock Syntax

Mock is a JavaScript library that generates random data and intercepts Ajax requests. This post will show you the common syntax of Mock, including generating random data, simulating data templates, and mocking APIs.

Mock is a JavaScript library that generates random data and intercepts Ajax requests.

This post will show you the common syntax of Mock, including generating random data, simulating data templates, and mocking APIs.

Mock Data

Generate Random Data

Mock.Random.boolean()

Generate a random boolean. For example:

Mock.Random.boolean()

The return value is true or false.

Mock.Random.integer(min, max)

Generate a random integer with a specified range. For example:

Mock.Random.integer(0, 10)

Returns an integer between 0 and 10.

Mock.Random.float(min, max, dmin, dmax)

Generate a random floating-point number with a specified range and precision. For example:

Mock.Random.float(0, 100, 2, 5)

Return a floating-point number between 0.00 and 100.00000 with precision between 2 and 5 decimal places.

Mock.Random.string(length)

Generate a random string with a specified length. For example:

Mock.Random.string(10)

Return a random string with a length of 10 characters.

Mock.Random.date()

Generate a random date. For example:

Mock.Random.date()

Return a string representation of a random date. For example:1977-07-03.

Mock.Random.time()

Generate a random time. For example:

Mock.Random.time()

Return a string representation of a random time. For example 05:38:02.

Mock.Random.datetime()

Generate a random datatime. For example:

Mock.Random.datetime()

Return a string representation of a random datatime. For example2007-06-29T22:03:06.140Z.

Mock.Random.image(size, background, foreground, format, text)

Generate a random image with a specified size, background color, foreground color, format, and text. For example:

Mock.Random.image('200x100', '#50B347', '#FFF', 'png', 'Hello, Mock.js!')

The return value is a Base64 encoded string of the image.

Generate Data Specified by the Schema.

Mock.mock(template)

Generate simulated data based on data templates. The schema can generate complex data structures.

The syntax for the schema is as follows:

  • Separate property names and property values with a colon :.
  • Separate properties with a comma,.
  • Property values can be strings and can contain placeholders represented by the @ symbol.
  • Property values can also be objects and can include generation rules represented by the syntax name|rule.

Example:

const Mock = require('mockjs')

const data = Mock.mock({
  'list|1-10': [{
    'id|+1': 1, // Starting from 1, increase by 1 each time
    'name': '@cname', // Random Chinese name
    'age|18-60': 1, // Integer between 18 and 60
    'gender|1': ['male', 'female'], // male or female
    'email': '@email' // Random email address
  }]
})

console.log(data)

Export:

{
"list": [
{
"id": 1,
"name": "Taylor Swift",
"age": 32,
"gender": "Female",
"email": "kbsc@pmpuaaq.pk"
},
{
"id": 2,
"name": "Jay Chou",
"age": 55,
"gender": "Male",
"email": "whq@zjfwq.uz"
},
...
]
}

Mock API

  • Mock.mock(url, template) Mock API requests based on the interface address and data template.
  • Mock.mock(method, url, template) Mock requests based on the request method, endpoint address, and data template.

The above is commonly used syntax in Mock, for more syntax please refer to the official documentation. Want to become a Mock master? Apidog is a powerful API documentation management tool that makes Mocking easier. Apidog syntax is fully compatible with Mock and extends some syntax that Mock doesn't have. Give it a try.

button