Mock.Random的运用

本文介绍了一种使用Mock库生成随机Web数据和地理数据的方法,包括随机域名、IP、URL、地理位置、文本、颜色、日期时间、图片及姓名等。通过调用库中的Random方法,可以生成符合特定格式的数据,适用于测试和数据填充场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模拟Web数据:

生成随机域名(每次运行结果不同):

var Random = Mock.Random
Random.domain()  //   "nhou.org.cn"

生成随机IP(每次运行结果不同)

var Random = Mock.Random
Random.ip()   //  "74.97.41.159"

生成随机URL(每次运行结果不同)

Random.url()  //   "news://wrmt.na/rbcgbws"

模拟地理位置数据: 

生成随机省份:

var Random = Mock.Random
Random.province()  //"海南省"

 

生成随机城市:

var Random = Mock.Random
Random.city()   // "澳门半岛"

 

 

生成在某个省份的某个城市:

var Random = Mock.Random
Random.city(true) // "广东省 广州市"

 

模拟文本数据:

 

生成一条随机的中文句子:

var Random = Mock.Random
Random.csentence()   //  "会候权以解包党心要按总场火义国而片精。"

【注意】

1.默认一条句子里的汉字个数在12和18之间

2. 通过Random.csentence( length )指定句子的汉字个数:

Random.csentence(5)  // "文斗领拉米。"

3.通过Random.csentence( min?, max? )指定句子汉字个数的范围:

Random.csentence(3, 5)  // "住验住"

 

生成随机的中文段落:

var Random = Mock.Random
Random.cparagraph()  
// "电力速率离老五准东其引是外适只王。体区先手天里己车发很指一照委争本。
   究利天易里根干铁多而提造干下志维。级素一门件一压路低表且太马。"

 

【注意】

  • cparagraph可以看作是多条csentence以逗号连接后的字符串,默认条数为 3 到 7条csentence
  • 通过Random.cparagraph(length )指定句子的个数
Random.cparagraph(2) 
// "而易除应精基还主局按选际复格从导。天第们国分比积造业王该回过白亲。"

4.通过Random.cparagraph(min?, max?)指定句子的个数的范围:

Random.cparagraph(1, 3)
//  "作养装军头确应当号天革来人车号把文。证细专物转民相解状律极或经较把马。
     其省级支际标业强龙算建物况。"

模拟颜色数据:

var Random = Mock.Random
Random.rgba()  // "rgba(122, 121, 242, 0.13)"

 

模拟日期/时间数据:

 

日期:

Random.date('yyyy-MM-dd')  // "1975-04-27"
Random.date('yy-MM-dd')    //   "00-01-08"

 

时间:

Random.time()   // "05:06:06"

模拟图片:

Random.image('200x100', '#4A7BF7', 'Hello')

 

不指定参数则取随机的宽高并显示对应的宽高数据:

模拟姓名数据:

 

模拟全名:

Random.cname()   // "黄秀英"

模拟姓氏:

Random.cfirst()   // "龙"

 

模拟名字

Random.clast()  // "秀英"

 

WebMergedContextConfiguration@355c94be testClass = com.example.dietsportmanage.GenerateApplicationTests, locations = [], classes = [com.example.DietsportmanageApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@2fba3fc4, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@1c852c0f, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@6b58b9e9, org.springframework.boot.test.web.reactor.netty.DisableReactorResourceFactoryGlobalResourcesContextCustomizerFactory$DisableReactorResourceFactoryGlobalResourcesContextCustomizerCustomizer@2b9ed6da, org.springframework.boot.test.autoconfigure.OnFailureConditionReportContextCustomizerFactory$OnFailureConditionReportContextCustomizer@421e361, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@7ef27d7f, org.springframework.test.context.support.DynamicPropertiesContextCustomizer@0, org.springframework.boot.test.context.SpringBootTestAnnotation@992bef3a], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
最新发布
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Embrace924

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值