mock

Mock简明文档

Mock.mock()

  • Mock.mock( requestUrl?, requestType?, template|function(options) )
  • Mock.mock( template )
  • Mock.mock( requestUrl, template )
  • Mock.mock( requestUrl, requestType, template )
  • Mock.mock( requestUrl, requestType, function(options) )

requestUrl: 要拦截的URL,字符串或正则表达式

equestType: 要拦截的请求类型,get/post/put/delete/options…

template: 数据模板

function(options):生成响应数据的函数,options --> { url, type, body }


语法规范

数据模板定义

数据模板中每个属性由3部分组成: 属性名|生成规则:属性值

  1. ‘name|min-max’: value
  2. ‘name|count’: value
  3. ‘name|min-max.dmin-dmax’: value
  4. ‘name|min-max.dcount’: value
  5. ‘name|count.dmin-dmax’: value
  6. ‘name|count.dcout’: value
  7. ‘name|+step’: value

属性值中可以包含@占位符
属性值还指定了最终值的初始值和类型

1.属性值是字符串
  1. ‘name|min-max’: string

通过重复 string 生成一个字符串,重复次数大于等于 min,小于等于 max

  1. ‘name|count’: string

通过重复 string 生成一个字符串,重复次数等于 count

2.属性值是数字
  1. ‘name|+1’: number

属性值自动加 1,初始值为 number

  1. ‘name|min-max’: number

生成一个大于等于 min、小于等于 max 的整数,属性值 number 只是用来确定类型

  1. ‘name|min-max.dmin-dmax’: number

生成一个浮点数,整数部分大于等于 min、小于等于 max,小数部分保留 dmin 到 dmax 位

3.属性值是布尔值
  1. ‘name|1’: boolean

随机生成一个布尔值,值为 true 的概率是 1/2,值为 false 的概率同样是 1/2

  1. ‘name|min-max’: value

随机生成一个布尔值,值为 value 的概率是 min / (min + max),值为 !value 的概率是 max / (min + max)

4.属性值是对象
  1. ‘name|count’: object

从属性值 object 中随机选取 count 个属性

  1. ‘name|min-max’: object

从属性值 object 中随机选取 min 到 max 个属性

5.属性值是数组
  1. ‘name|1’: array

从属性值 array 中随机选取 1 个元素,作为最终值

  1. ‘name|+1’: array

从属性值 array 中顺序选取 1 个元素,作为最终值

  1. ‘name|min-max’: array

通过重复属性值 array 生成一个新数组,重复次数大于等于 min,小于等于 max

  1. ‘name|count’: array

通过重复属性值 array 生成一个新数组,重复次数为 count

6.属性值是函数
  1. ‘name’: function

执行函数 function,取其返回值作为最终的属性值,函数的上下文为属性 ‘name’ 所在的对象

7.属性值是正则表达式
  1. ‘name’: regexp

根据正则表达式 regexp 反向生成可以匹配它的字符串。用于生成自定义格式的字符串

数据占位符定义

  1. 用 @ 来标识其后的字符串是 占位符
  2. 占位符 引用的是 Mock.Random 中的方法
  3. 通过 Mock.Random.extend() 来扩展自定义占位符
  4. 占位符 也可以引用 数据模板 中的属性
  5. 占位符 会优先引用 数据模板 中的属性
  6. 占位符 支持 相对路径 和 绝对路径

Mock.setup()

配置Ajax请求的行为,暂时支持的配置项有timeout

  Mock.setup({
    timeout: 500
  })
  Mock.setup({
    timeout: '100-600'
  })

Mock.Random

  const Random = Mock.Random
  Random.email() // => sd.sdf@oksd.com
  Mock.mock('@email') // => sd.sdf@oksd.com
  Mock.mock({ email: 'sd.sdf@oksd.com' }) // => { email: "sd.sdf@oksd.com" }

Mock.Random提供的完整方法(占位符):

TypeMethod
Basicboolean, natural, integer, float, character, string, range, date, time, datetime, now
Imageimage, dataImage
Colorcolor
Textparagraph, sentence, word, title, cparagraph, csentence, cword, ctitle
Namefirst, last, name, cfirst, clast, cname
Weburl, domain, email, ip, tld
Addressarea(region, province, city(bool), county(bool), zip), region
Helpercapitalize(首字母大写), upper(大写), lower(小写), pick(从数组任取一个), shuffle(打乱数组元素顺序)
Miscellaneousguid, id
Basic
  1. Random.boolean(min?, max?, current? )
  2. Random.natural(min?, max? )
  3. Random.integer(min?, max? )
  4. Random.float( min?, max?, dmin?, dmax? )
  5. Random.character( pool? ) // pool => lower/upper/number/symbol
  6. Random.string( pool?, min?, max? ) // pool => lower/upper/number/symbol
  7. Random.range( start?, stop, step? )
Date
  1. Random.date( format? )
FormatDescriptionExample
yyyyA full numeric representation of a year, 4 digits1999 or 2003
yyA two digit representation of a year99 or 03
yA two digit representation of a year99 or 03
MMNumeric representation of a month, with leading zeros01 to 12
MNumeric representation of a month, without leading zeros1 to 12
ddDay of the month, 2 digits with leading zeros01 to 31
dDay of the month without leading zeros1 to 31
HH24-hour format of an hour with leading zeros00 to 23
H24-hour format of an hour without leading zeros0 to 23
hh12-hour format of an hour without leading zeros01 to 12
h12-hour format of an hour with leading zeros1 to 12
mmMinutes, with leading zeros00 to 59
mMinutes, without leading zeros0 to 59
ssSeconds, with leading zeros00 to 59
sSeconds, without leading zeros0 to 59
SSMilliseconds, with leading zeros000 to 999
SMilliseconds, without leading zeros0 to 999
AUppercase Ante meridiem and Post meridiemAM or PM
aLowercase Ante meridiem and Post meridiemam or pm
TMilliseconds, since 1970-1-1 00:00:00 UTC759883437303
  1. Random.time( format? )
  2. Random.datetime( format? )
  3. Random.now( unit?, format? ) // unit => year、month、week、day、hour、minute、second、week
Image
Random.image()
  1. Random.image()
  2. Random.image( size )
  3. Random.image( size, background )
  4. Random.image( size, background, text )
  5. Random.image( size, background, foreground, text )
  6. Random.image( size, background, foreground, format, text )
Random.dataImage()
  1. Random.dataImage()
  2. Random.dataImage( size )
  3. Random.dataImage( size, text )
Color
  1. Random.color() // => #3538B2
  2. Random.hex() // => #3538B2
  3. Random.rgb() // => rgb(242, 198, 121)
  4. Random.rgba() // => rgba(242, 198, 121, 0.13)
  5. Random.hsl() // => hsl(345, 82, 71)
Text
  1. Random.paragraph( min?, max? )
  2. Random.cparagraph( min?, max? )
  3. Random.sentence( min?, max? )
  4. Random.csentence( min?, max? )
  5. Random.word( min?, max? )
  6. Random.cword( pool?, min?, max? )
  7. Random.title( min?, max? )
  8. Random.ctitle( min?, max? )
Name
  1. Random.first()
  2. Random.last()
  3. Random.name( middle? )
  4. Random.cfirst()
  5. Random.clast()
  6. Random.cname()
Web
  1. Random.url( protocol?, host? )
  2. Random.protocol()
  3. Random.domain() // 域名
  4. Random.tld() // 顶级域名
  5. Random.email( domain? )
  6. Random.ip()
Address
  1. Random.region()
  2. Random.province()
  3. Random.city( prefix? )
  4. Random.county( prefix? )
  5. Random.zip()
Helper
  1. Random.capitalize( word )
  2. Random.upper( str )
  3. Random.lower( str )
  4. Random.pick( arr )
  5. Random.shuffle( arr )
Miscellaneous
  1. Random.guid()
  2. Random.id()
  3. Random.increment( step? )

扩展

Random.extend({
  fruit () {
    const fruit = ['apple', 'peach', 'lemon']
    return this.pick(fruit)
  }
})
Random.fruit() // => 'peach'
Mock.mock('@fruit')  // => 'lemon'
Mock.mock({
  fruit: '@fruit' // => 'peach'
})

Mock.valid()

Mock.valid( template, data )

Mock.toJSONSchema()

Mock.toJSONSchema( template )
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值