Mock簡明文檔

轉載自: vue技術棧開發實戰

https://segmentfault.com/ls/1650000016221751

iview-admin作者 Lison 出品

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,字符串或正則表達式<br>
equestType: 要攔截的請求類型,get/post/put/delete/options...<br>
template: 數據模板<br>
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

  2. 'name|count': string

    通過重複 string 生成一個字符串,重複次數等於 count

2.屬性值是數字

  1. 'name|+1': number

    屬性值自動加 1,初始值爲 number

  2. 'name|min-max': number

    生成一個大於等於 min、小於等於 max 的整數,屬性值 number 只是用來確定類型

  3. 'name|min-max.dmin-dmax': number

    生成一個浮點數,整數部分大於等於 min、小於等於 max,小數部分保留 dmin 到 dmax 位

3.屬性值是布爾值

  1. 'name|1': boolean

    隨機生成一個布爾值,值爲 true 的概率是 1/2,值爲 false 的概率同樣是 1/2

  2. 'name|min-max': value

    隨機生成一個布爾值,值爲 value 的概率是 min / (min + max),值爲 !value 的概率是 max / (min + max)

4.屬性值是對象

  1. 'name|count': object

    從屬性值 object 中隨機選取 count 個屬性

  2. 'name|min-max': object

    從屬性值 object 中隨機選取 min 到 max 個屬性

5.屬性值是數組

  1. 'name|1': array

    從屬性值 array 中隨機選取 1 個元素,作爲最終值

  2. 'name|+1': array

    從屬性值 array 中順序選取 1 個元素,作爲最終值

  3. 'name|min-max': array

    通過重複屬性值 array 生成一個新數組,重複次數大於等於 min,小於等於 max

  4. '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() // => [email protected]
  Mock.mock('@email') // => [email protected]
  Mock.mock({ email: '[email protected]' }) // => { email: "[email protected]" }

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(從數組任取一個), shuffle(打亂數組元素順序)
Miscellaneous guid, 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? )
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
  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 )

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章