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: 數(shù)據(jù)模板
function(options):生成響應(yīng)數(shù)據(jù)的函數(shù),options --> { url, type, body }
語法規(guī)范
數(shù)據(jù)模板定義
數(shù)據(jù)模板中每個屬性由3部分組成: 屬性名|生成規(guī)則:屬性值
- '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.dcout': value
- 'name|+step': value
屬性值中可以包含@占位符
屬性值還指定了最終值的初始值和類型
1.屬性值是字符串
- 'name|min-max': string
通過重復(fù) string 生成一個字符串,重復(fù)次數(shù)大于等于 min,小于等于 max
- 'name|count': string
通過重復(fù) string 生成一個字符串,重復(fù)次數(shù)等于 count
2.屬性值是數(shù)字
- 'name|+1': number
屬性值自動加 1,初始值為 number
- 'name|min-max': number
生成一個大于等于 min、小于等于 max 的整數(shù),屬性值 number 只是用來確定類型
- 'name|min-max.dmin-dmax': number
生成一個浮點數(shù),整數(shù)部分大于等于 min、小于等于 max,小數(shù)部分保留 dmin 到 dmax 位
3.屬性值是布爾值
- 'name|1': boolean
隨機生成一個布爾值,值為 true 的概率是 1/2,值為 false 的概率同樣是 1/2
- 'name|min-max': value
隨機生成一個布爾值,值為 value 的概率是 min / (min + max),值為 !value 的概率是 max / (min + max)
4.屬性值是對象
- 'name|count': object
從屬性值 object 中隨機選取 count 個屬性
- 'name|min-max': object
從屬性值 object 中隨機選取 min 到 max 個屬性
5.屬性值是數(shù)組
- 'name|1': array
從屬性值 array 中隨機選取 1 個元素,作為最終值
- 'name|+1': array
從屬性值 array 中順序選取 1 個元素,作為最終值
- 'name|min-max': array
通過重復(fù)屬性值 array 生成一個新數(shù)組,重復(fù)次數(shù)大于等于 min,小于等于 max
- 'name|count': array
通過重復(fù)屬性值 array 生成一個新數(shù)組,重復(fù)次數(shù)為 count
6.屬性值是函數(shù)
- 'name': function
執(zhí)行函數(shù) function,取其返回值作為最終的屬性值,函數(shù)的上下文為屬性 'name' 所在的對象
7.屬性值是正則表達式
- 'name': regexp
根據(jù)正則表達式 regexp 反向生成可以匹配它的字符串。用于生成自定義格式的字符串
數(shù)據(jù)占位符定義
- 用 @ 來標識其后的字符串是 占位符
- 占位符 引用的是 Mock.Random 中的方法
- 通過 Mock.Random.extend() 來擴展自定義占位符
- 占位符 也可以引用 數(shù)據(jù)模板 中的屬性
- 占位符 會優(yōu)先引用 數(shù)據(jù)模板 中的屬性
- 占位符 支持 相對路徑 和 絕對路徑
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提供的完整方法(占位符):
Type | Method |
---|---|
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, province, city(bool), county(bool), zip), region |
Helper | capitalize(首字母大寫), upper(大寫), lower(小寫), pick(從數(shù)組任取一個), shuffle(打亂數(shù)組元素順序) |
Miscellaneous | guid, id |
Basic
- Random.boolean(min?, max?, current? )
- Random.natural(min?, max? )
- Random.integer(min?, max? )
- Random.float( min?, max?, dmin?, dmax? )
- Random.character( pool? ) // pool => lower/upper/number/symbol
- Random.string( pool?, min?, max? ) // pool => lower/upper/number/symbol
- Random.range( start?, stop, step? )
Date
- Random.date( format? )
Format | Description | Example |
---|---|---|
yyyy | A full numeric representation of a year, 4 digits | 1999 or 2003 |
yy | A two digit representation of a year | 99 or 03 |
y | A two digit representation of a year | 99 or 03 |
MM | Numeric representation of a month, with leading zeros | 01 to 12 |
M | Numeric representation of a month, without leading zeros | 1 to 12 |
dd | Day of the month, 2 digits with leading zeros | 01 to 31 |
d | Day of the month without leading zeros | 1 to 31 |
HH | 24-hour format of an hour with leading zeros | 00 to 23 |
H | 24-hour format of an hour without leading zeros | 0 to 23 |
hh | 12-hour format of an hour without leading zeros | 01 to 12 |
h | 12-hour format of an hour with leading zeros | 1 to 12 |
mm | Minutes, with leading zeros | 00 to 59 |
m | Minutes, without leading zeros | 0 to 59 |
ss | Seconds, with leading zeros | 00 to 59 |
s | Seconds, without leading zeros | 0 to 59 |
SS | Milliseconds, with leading zeros | 000 to 999 |
S | Milliseconds, without leading zeros | 0 to 999 |
A | Uppercase Ante meridiem and Post meridiem | AM or PM |
a | Lowercase Ante meridiem and Post meridiem | am or pm |
T | Milliseconds, since 1970-1-1 00:00:00 UTC | 759883437303 |
- Random.time( format? )
- Random.datetime( format? )
- Random.now( unit?, format? ) // unit => year、month、week、day、hour、minute、second、week
Image
Random.image()
- Random.image()
- Random.image( size )
- Random.image( size, background )
- Random.image( size, background, text )
- Random.image( size, background, foreground, text )
- Random.image( size, background, foreground, format, text )
Random.dataImage()
- Random.dataImage()
- Random.dataImage( size )
- Random.dataImage( size, text )
Color
- Random.color() // => #3538B2
- Random.hex() // => #3538B2
- Random.rgb() // => rgb(242, 198, 121)
- Random.rgba() // => rgba(242, 198, 121, 0.13)
- Random.hsl() // => hsl(345, 82, 71)
Text
- Random.paragraph( min?, max? )
- Random.cparagraph( min?, max? )
- Random.sentence( min?, max? )
- Random.csentence( min?, max? )
- Random.word( min?, max? )
- Random.cword( pool?, min?, max? )
- Random.title( min?, max? )
- Random.ctitle( min?, max? )
Name
- Random.first()
- Random.last()
- Random.name( middle? )
- Random.cfirst()
- Random.clast()
- Random.cname()
Web
- Random.url( protocol?, host? )
- Random.protocol()
- Random.domain() // 域名
- Random.tld() // 頂級域名
- Random.email( domain? )
- Random.ip()
Address
- Random.region()
- Random.province()
- Random.city( prefix? )
- Random.county( prefix? )
- Random.zip()
Helper
- Random.capitalize( word )
- Random.upper( str )
- Random.lower( str )
- Random.pick( arr )
- Random.shuffle( arr )
Miscellaneous
- Random.guid()
- Random.id()
- 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'
})