接上篇Fake data文章 - Python篇。本文主要介紹基于JavaScript的fake data generator。
API https://randomuser.me/
randomuser.me提供一個(gè)APIhttps://randomuser.me/api/來(lái)返回隨機(jī)用戶(hù)數(shù)據(jù),免費(fèi)且十分方便好用。下面我們來(lái)看如何使用這個(gè)API。
多格式
簡(jiǎn)單的一個(gè)GET請(qǐng)求https://randomuser.me/api/即可獲得包含用戶(hù)數(shù)據(jù)的response,默認(rèn)格式是JSON,但通過(guò)?format=xxx參數(shù)我們可以指定不同的數(shù)據(jù)格式,目前支持的有
- JSON (default)
- PrettyJSON or pretty
- CSV
- YAML
- XML
結(jié)合chakram我們來(lái)看看這個(gè)API如何使用
var chakram = require('./node_modules/chakram/lib/chakram.js'),
expect = chakram.expect;
const randomuserURL = "https://randomuser.me/api/";
describe("try random user API in different format", function () {
it("should return JSON", function () {
return chakram.get(randomuserURL).then(function(response){
console.log(response.body);
expect(response).to.have.status(200);
});
});
it("should return YAML", function () {
return chakram.get(randomuserURL + "?format=yaml").then(function(response){
console.log(response.body);
expect(response).to.have.status(200);
});
});
it("should return XML", function () {
return chakram.get(randomuserURL + "?format=xml").then(function(response){
console.log(response.body);
expect(response).to.have.status(200);
});
});
});
多用戶(hù)
?results=5000參數(shù)可以一次性返回多個(gè)用戶(hù)數(shù)據(jù)。上限是5000。
var chakram = require('./node_modules/chakram/lib/chakram.js'),
expect = chakram.expect;
const randomuserURL = "https://randomuser.me/api/";
describe("try random user API", function () {
it("should two prettyJSON", function () {
return chakram.get(randomuserURL + "?format=pretty&results=2").then(function(response){
console.log(response.body);
expect(response).to.have.status(200);
});
});
});
定制性別相關(guān)數(shù)據(jù)
?gender=male或?gender=female可以返回?cái)?shù)據(jù)去接近于male還是female
var chakram = require('./node_modules/chakram/lib/chakram.js'),
expect = chakram.expect;
const randomuserURL = "https://randomuser.me/api/";
describe("try random user API", function () {
it("should return male", function () {
return chakram.get(randomuserURL + "?gender=male").then(function(response){
console.log(response.body);
expect(response).to.have.status(200);
});
});
});
產(chǎn)生一樣的數(shù)據(jù)
?seed=danteyu可以讓每次執(zhí)行產(chǎn)生一致的數(shù)據(jù)。Seeds可以為任意字符串。比如下面的代碼重復(fù)執(zhí)行會(huì)產(chǎn)生一致的結(jié)果。
var chakram = require('./node_modules/chakram/lib/chakram.js'),
expect = chakram.expect;
const randomuserURL = "https://randomuser.me/api/";
describe("try random user API", function () {
it("should seed", function () {
return chakram.get(randomuserURL + "?seed=danteyu").then(function(response){
console.log(response.body);
expect(response).to.have.status(200);
});
});
});
Locale
locale可以通過(guò)?nat=us。通過(guò)設(shè)置locale,可以返回更加真實(shí)的適用于不同國(guó)籍的數(shù)據(jù)。
var chakram = require('./node_modules/chakram/lib/chakram.js'),
expect = chakram.expect;
const randomuserURL = "https://randomuser.me/api/";
describe("try random user API", function () {
it("should return CH", function () {
return chakram.get(randomuserURL + "?nat=ch").then(function(response){
console.log(response.body.results[0]);
expect(response).to.have.status(200);
});
});
});
刷選特定數(shù)據(jù)
如果只是想要返回特定的幾個(gè)數(shù)據(jù)可以使用?inc=gender,name,nat來(lái)包含只需要返回的數(shù)據(jù)或是?exc=login來(lái)排除不想要的數(shù)據(jù)。
var chakram = require('./node_modules/chakram/lib/chakram.js'),
expect = chakram.expect;
const randomuserURL = "https://randomuser.me/api/";
describe("try random user API", function () {
it("should return name and gender", function () {
return chakram.get(randomuserURL + "?inc=name,gender").then(function(response){
console.log(response.body);
expect(response).to.have.status(200);
});
});
});
使用?noinfo可以只返回用戶(hù)數(shù)據(jù)而沒(méi)有seed,result和version等數(shù)據(jù)。
var chakram = require('./node_modules/chakram/lib/chakram.js'),
expect = chakram.expect;
const randomuserURL = "https://randomuser.me/api/";
describe("try random user API", function () {
it("should return only user data", function () {
return chakram.get(randomuserURL + "?noinfo").then(function(response){
console.log(response.body.results);
expect(response).to.have.status(200);
});
});
});
下面為輸出
> randomuser@1.0.0 test /Users/diyu/workspace/chakram
> mocha demo.js
try random user API
[ { gender: 'male',
name: { title: 'mr', first: 'ryan', last: 'mackay' },
location:
{ street: '6297 richmond ave',
city: 'stratford',
state: 'new brunswick',
postcode: 77478 },
email: 'ryan.mackay@example.com',
login:
{ username: 'heavywolf629',
password: 'method',
salt: 'EVZMskM7',
md5: '6756ee051abaccca0214c997204d8d67',
sha1: '4c265c506210e365c9e1acadcdc2321fe899d8c4',
sha256: '90dfbb565010f9524be4531ec8fc712aad3fca69658976c8dc7615a84afa86b9' },
dob: '1979-08-06 17:27:58',
registered: '2002-04-02 00:35:37',
phone: '448-153-9250',
cell: '979-394-3188',
id: { name: '', value: null },
picture:
{ large: 'https://randomuser.me/api/portraits/men/62.jpg',
medium: 'https://randomuser.me/api/portraits/med/men/62.jpg',
thumbnail: 'https://randomuser.me/api/portraits/thumb/men/62.jpg' },
nat: 'CA' } ]
? should return only user data (990ms)
1 passing (999ms)
Faker
類(lèi)似python的faker提供了各類(lèi)各樣數(shù)據(jù)。Faker的使用非常簡(jiǎn)單,直接引用調(diào)用就行了。下面例子列舉了部分常用數(shù)據(jù)類(lèi)型。
const faker = require('faker');
//address
console.log(faker.address.zipCode());
console.log(faker.address.city());
//commerce
console.log(faker.commerce.product());
console.log(faker.commerce.department());
//company
console.log(faker.company.companyName());
console.log(faker.company.bsBuzz());
//date
console.log(faker.date.future());
console.log(faker.date.month());
console.log(faker.date.weekday());
//finance
console.log(faker.finance.account());
console.log(faker.finance.transactionType());
//internet
console.log(faker.internet.email());
console.log(faker.internet.ip());
//lorem
console.log(faker.lorem.words());
console.log(faker.lorem.text());
//name
console.log(faker.name.findName());
console.log(faker.name.jobTitle());
//phone
console.log(faker.phone.phoneNumber());
Faker.fake()可以接受不同的API來(lái)組合字符串。
const faker = require('faker');
console.log(faker.fake("{{company.companyName}}, {{internet.email}} {{phone.phoneNumber}}"))
同樣地,faker也支持多個(gè)locale。通過(guò)下面代碼,可以進(jìn)行設(shè)置。
const faker = require('faker');
faker.locale = "zh_CN";
faker也支持seed()來(lái)產(chǎn)生一致的數(shù)據(jù),這樣的數(shù)據(jù)多用于單元測(cè)試等場(chǎng)景中。重復(fù)執(zhí)行下面的代碼會(huì)產(chǎn)生一致的結(jié)果。
const faker = require('faker');
faker.seed(123);
console.log(faker.company.companyName());
faker也有CLI和JSON Schema對(duì)應(yīng)的實(shí)現(xiàn)。