mock.js use

A: Concepts

Mock.js is a simulation data generator designed to help front-end siege division independent of the back-end development, help write unit tests. Analog functions provides the following:

  • Generating simulation data from the data template
  • Analog Ajax request, generate and return the analog data
  • Generate analog data based on HTML templates

Official website: http://mockjs.com/

II: Installation

cnpm install mockjs

Three: specification data template definition DTD

Attribute data for each template consists of three parts: attribute name generation rule, an attribute value

// property name name
// generation rules rule
// property value value
'name | rule': value

Separated | by a vertical bar between attribute names and generation rules. Generation rule is optional. There are seven format generation rules:

'name|min-max': value
'name|count': value
'name|min-max.dmin-dmax': value
'name|min-max.dcount': value
'name|count.dmin-dmax': value
'name|count.dcount': value
'name|+step': value

Generating meaning of the rules need to rely on the type of property values ​​can be determined. @ Attribute value may contain placeholders. Attribute value also specifies the type of the initial value and final value.

// 使用 Mock
let Mock = require('mockjs')
let data = Mock.mock({
    'list|5‐10': [{
        'id|+1': 1,
        'name|2‐3':'测试',
        'phone|11':'1',
        'point|122‐500':0,
        'money|3000‐8000.2':0,
        'status|1':true,
        'default|1‐3':true,
        'detail|2‐3':{'id':1,'date':'2005‐01‐01','content':'记录'}
    }]
})
// 输出结果
console.log(JSON.stringify(data,null,2))

1. The attribute value is string

'name|count': string: 'Phone | 11': ' 1'
generated by repeating a string string, count the number of repetitions is equal to

'name|min-max': string: 'Name | 2-4': 'test'
generated by repeating a string string, the number of repetitions is greater than equal to min, max or less

2. The number attribute value is

'name|+1': number: 'Id | +1': 1
attribute value is automatically incremented by 1, the initial value of number

'name|min-max': number: 'Point | 122-500': 0
generates a greater than or equal min, max is less than an integer, the value of the attribute number is only used to determine the type

'name|min-max.dcount': value: 'Money | 3000-8000.2': 0
generates a floating-point, integer greater than or equal portions min, less max, bit fractional part of dcount

'name|min-max.dmin-dmax': number: 'Money2 | 1000-5000.2-4': 0
generates a floating-point, integer greater than or equal portions min, less max, dmin to dmax fractional part reserved bits

3. The attribute value is boolean

'name|1': boolean: 'Status | 1': true
randomly generates a boolean value of true probability is 1/2, the probability of false value is also 1/2

'name|min-max': value: 'Default | 1-3': true
randomly generates a Boolean value, the probability value is a value of min / (min + max)

4. Object attribute value is

'name|count': object: 'Detail | 2': { 'id': 1, 'date': '2005-01-01', 'content': ' record'}
from the attribute values of object attributes selected at random count

'name|min-max': object: 'Detail | 2-3': { 'id': 1, 'date': '2005-01-01', 'content': ' record'}
randomly selected from min to max attribute value of the object attributes

The property value array

'name|count': array
By repeating the property value array to generate a new array, count the number of repetitions of

'name|min-max': array: 'List | 5-10': [ {...}]
By repeating the property value array to generate a new array, the number of repetitions min greater than or equal, less than equal to max

IV: Data placeholder define specifications DPD

Mock.Random is a utility class used to generate a variety of random data. Mock.Random method called "placeholder" in the data template, the writing format @ placeholder (parameter [, parameter]). Built-in method list:

Basic: boolean,natural,integer,float,character,string,range,date,time,datetime,now
Image: image, dataImage
Color: color
Text: paragraph, sentence, word, title, cparagraph, csentence,cword, ctitle
Name: first, last, name, cfirst, clast, cname
Web: url, domain, email, ip, tld
Address: area, region,county
Helper: capitalize, upper, lower, pick, shuffle
Miscellaneous: guid, id

Example:

// 使用 Mock
let Mock = require('mockjs')
let data = Mock.mock({
    'list|10': [{
        'id|+1': 1,
        'name':'@cname',
        'ename':'@last',
        'cfirst':'@cfirst',
        'point':'@integer',
        'birthday':'@date',
        'pic':'@image',
        'title':'@title',
        'content':'@cword(100)',
        'url':"@url",
        'ip':"@ip",
        'email':"@email",
        'area':'@region',
        'address':'@county(true)'
    }]
})
// 输出结果
console.log(JSON.stringify(data,null,2))

1. Basic Method

string 字符串: 'Name': '@ String'
integer 整数: 'Point': 'Integer @'
date 日期: 'Birthday': '@ DATE'
may generate a random basic data types

2. The method of image

'pic':'@image'
image pictures randomly generated address

3. Text method

@title: 标题 : 'title':'@title'
@cword(100) :文本内容 参数为字数 : 'content':'@cword(100)'

4. Method Name

cname :中文名称 : 'name':'@cname'
cfirst:中文姓氏 : 'cfirst':'@cfirst'
Last:英文姓氏 : 'ename':'@last'

5. Network

Url ip email may be generated network-related information, etc.
'url':"@url"
'ip':"@ip"
'email':"@email"

5. The method of address

@region 区域 : 'area':'@region'
@county 省市县 : 'address':'@county(true)'

Guess you like

Origin www.cnblogs.com/itzlg/p/11966829.html