博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
faker 测试数据生成_使用Faker为JavaScript应用程序生成伪数据
阅读量:2507 次
发布时间:2019-05-11

本文共 9232 字,大约阅读时间需要 30 分钟。

faker 测试数据生成

介绍 (Introduction)

When building out applications, we will often need data to show off how it works. It won't be much of an application if all our tables are blank!

在构建应用程序时,我们经常需要数据来展示其工作原理。 如果我们所有的表都为空,那将不会是什么应用程序!

Often, when we first start building out an application, we don't want to worry too much about the data. Having to create a database and fill it with sample data like users is an annoying step that is just in the way of creating an amazing app.

通常,当我们第一次开始构建应用程序时,我们不想过多地担心数据。 必须创建一个数据库并将其填充为用户之类的示例数据,这是一个令人讨厌的步骤,而这只是创建一个了不起的应用程序的方式。

Luckily has created a neat package that lets us create fake data on the fly: .

幸运的是, 创建了一个简洁的程序包,可让我们即时创建虚假数据: 。

一个快速的Faker示例 (A Quick Faker Example)

Let's say we wanted to create a user on the fly.

假设我们要动态创建一个用户。

We want this user to have a name, email, address, bio, and even an image.

我们希望该用户具有姓名电子邮件地址 ,个人简历 ,甚至是图片

In Node, all we would have to do is:

在Node中,我们要做的就是:

var user = {  name: faker.name.findName(),  email: faker.internet.email(),  address: faker.address.streetAddress(),  bio: faker.lorem.sentence(),  image: faker.image.avatar()};

Just like that, we have a whole user object that we can use within our application! The above will spit out:

就像那样,我们有一个可以在我们的应用程序中使用的整个用户对象! 以上将吐出:

{  "name": "Adaline Carroll",  "email": "Albina.Ledner@gmail.com",  "address": "545 Fritsch Locks",  "bio": "dolorem officiis doloribus ut",  "image": "https://s3.amazonaws.com/uifaces/faces/twitter/joelhelin/128.jpg"}

Now we have a user and can move along with our application. No need to fuss about the database or populating it with fake users!

现在我们有了一个用户,可以随我们的应用程序一起移动。 无需大惊小怪的数据库或用假用户填充它!

Let's learn a bit more about how to use this helpful package.

让我们进一步了解如何使用此有用的软件包。

使用Faker (Using Faker)

You can use faker from within your browser or on the server with Node.

您可以从浏览器内部或带有Node的服务器上使用伪造者。

From the , the installation instructions for both browser and Node are:

在 ,针对浏览器和Node的安装说明如下:

浏览器 (Browser)

节点 (Node)

var faker = require('faker');  var name = faker.name.findName();  var randomEmail = faker.internet.email();

Super easy stuff. Let's run through the list of items that we can pull.

超级容易的东西。 让我们浏览一下我们可以拉出的项目列表。

法克数据 (Faker Data)

There is a huge amount of data that is able to be pulled. The overall category list is:

有大量数据可以提取。 总体类别列表为:

  • name

    名称
  • address

    地址
  • phone

    电话
  • internet

    互联网
  • company

    公司
  • image

    图片
  • lorem

    洛雷姆
  • helpers

    帮手
  • date

    日期
  • random

    随机
  • hacker

    黑客

Each of those categories has a giant amount of options within itself. We won't go through them all, but an example is that for the name category, the following are available:

这些类别中的每一个都有大量的选择。 我们不会全部介绍它们,但是一个示例是,对于名称类别,以下内容可用:

  • name
    • firstName
    • lastName
    • findName
    • prefix
    • suffix

    名称
    • 名字
    • findName
    • 字首
    • 后缀

Each of these are accessible as a function. For instance:

这些功能均可以访问。 例如:

// Chris  var firstName = faker.name.firstName();  // Sevilleja  var lastName = faker.name.lastName();  // Chris Sevilleja  var findName = faker.name.findName();  // Mr.  var prefix = faker.name.prefix();  // PhD  var suffix = faker.name.suffix();

For a giant list of all the calls that are available through faker, check out the .

有关通过造假者可用的所有调用的庞大列表,请查看 。

帮手 (Helpers)

In addition to the many categories provided, faker also gives access to helpers. These are meant to make pulling data easier. Instead of picking one field here or there, these helpers can give a large amount of data that one would typically use in an application.

除了提供的许多类别之外,fakerr还可以访问帮助者。 这些是为了使提取数据更加容易。 这些帮助程序可以选择在一个应用程序中通常使用的大量数据,而不必在此处或此处选择一个字段。

Here we'll give examples of the main helpers that are usable in our own applications:

在这里,我们将提供在我们自己的应用程序中可用的主要帮助程序的示例:

fakers.helpers.contextualCard() (fakers.helpers.contextualCard())

A user card that contains name, username, avatar, email, and address.

包含名称用户名头像电子邮件地址的用户卡。

{  "name": "Kaleigh",  "username": "Kaleigh60",  "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/mbilderbach/128.jpg",  "email": "Kaleigh6047@gmail.com",  "dob": "1961-08-07T02:37:45.068Z",  "phone": "113.601.0680",  "address": {    "street": "Myah Course",    "suite": "Apt. 657",    "city": "Boyerberg",    "zipcode": "66413-8920",    "geo": {      "lat": "-44.6203",      "lng": "16.7454"    }  },  "website": "megane.biz",  "company": {    "name": "Little-Reinger",    "catchPhrase": "Enhanced regional emulation",    "bs": "interactive generate bandwidth"  }}

fakers.helpers.userCard() (fakers.helpers.userCard())

A smaller card with less user information. This one doesn't come with a user avatar.

较小的卡,用户信息较少。 这个没有用户头像。

{  "name": "George Kohler",  "username": "Emilio_Grady49",  "email": "Aurore_Wintheiser90@gmail.com",  "address": {    "street": "Corwin Cove",    "suite": "Suite 550",    "city": "New Myah",    "zipcode": "57817",    "geo": {      "lat": "-3.4045",      "lng": "-16.7806"    }  },  "phone": "1-806-338-0724 x844",  "website": "katheryn.net",  "company": {    "name": "Purdy, Lesch and Jacobi",    "catchPhrase": "Programmable incremental frame",    "bs": "24/365 optimize initiatives"  }}

fakers.helpers.createTransaction() (fakers.helpers.createTransaction())

{  "amount": "757.24",  "date": "2012-02-02T08:00:00.000Z",  "business": "Bahringer Group",  "name": "Home Loan Account 5279",  "type": "deposit",  "account": "63843216"}

faker.helpers.createCard() (faker.helpers.createCard())

The full might of the user card along with a section called posts that could help simulate blog articles.

用户卡的全部功能以及称为帖子的部分可以帮助模拟博客文章。

{  "name": "Fern Ritchie",  "username": "Vladimir.Feest",  "email": "Llewellyn_Robel@gmail.com",  "address": {    "streetA": "Ratke Street",    "streetB": "431 Terry Prairie",    "streetC": "027 Tyler Plaza Apt. 166",    "streetD": "Apt. 095",    "city": "Crooksstad",    "state": "New Hampshire",    "country": "Ukraine",    "zipcode": "53303",    "geo": {      "lat": "-5.9355",      "lng": "20.5942"    }  },  "phone": "477-849-4756 x71564",  "website": "emile.org",  "company": {    "name": "Stehr Inc",    "catchPhrase": "Universal 6th generation contingency",    "bs": "granular whiteboard bandwidth"  },  "posts": [{    "words": ["voluptatem", "quia", "ipsam"],    "sentence": "maxime et nemo et sed",    "sentences": "blanditiis sint molestiae facilisnet molestiae velit consequatur sit aut iure nihilneaque quo voluptatibus",    "paragraph": "reiciendis exercitationem assumenda provident odionasperiores fugit officianperferendis exercitationem annam et eana ex autem ducimusnsint tenetur officia ratione nesciunt impedit qui"  }, {    "words": ["voluptatem", "fugiat", "laborum"],    "sentence": "quis vel consequatur placeat eum voluptas laboriosam",    "sentences": "natus ut assumenda esse rerum repudiandaentempore excepturi nemo et velit inventore minus nihil officiisnexcepturi corrupti enim explicabo quaerat",    "paragraph": "sint eum tenetur id occaecatinitaque veritatis aperiam libero quibusdam repudiandae explicabo accusamus rerum inventorenet placeat enimndolor sit dolores nulla doloribus"  }, {    "words": ["illum", "quia", "voluptas"],    "sentence": "libero eos quia velit qui sint aut",    "sentences": "eligendi esse blanditiis magni perferendis cumque quibusdam qui iste vitaenaliquam odio aut quaeratnveritatis consequatur qui consequuntur",    "paragraph": "qui facere ex et consequaturnut iusto maxime quia deleniti sint autnut inventore magni ipsum necessitatibus et numquam qui consectetur aperiamnnam non quisquam tempore corporis adipisci nobis remnqui doloribus inventore itaque est corrupti modi vero quidem quaerat"  }],  "accountHistory": [{    "amount": "435.13",    "date": "2012-02-02T08:00:00.000Z",    "business": "Dare Inc",    "name": "Home Loan Account 4225",    "type": "payment",    "account": "69883581"  }, {    "amount": "993.77",    "date": "2012-02-02T08:00:00.000Z",    "business": "Yundt, VonRueden and Marks",    "name": "Credit Card Account 2737",    "type": "withdrawal",    "account": "32957725"  }, {    "amount": "844.46",    "date": "2012-02-02T08:00:00.000Z",    "business": "Sipes-Boyer",    "name": "Investment Account 5882",    "type": "deposit",    "account": "50708763"  }]}

在示例节点API中使用 (Using in a Sample Node API)

We can create a quick Node API that returns fake data. As an example, if we wanted an API endpoint ot return user data, we can create:

我们可以创建一个返回虚假数据的快速Node API。 例如,如果我们希望API端点返回用户数据,则可以创建:

app.get('/api/user', function(req, res) {  res.json({    name: faker.name.findName(),    email: faker.internet.email(),    address: faker.address.streetAddress(),    bio: faker.lorem.sentence(),    image: faker.image.avatar()  };});

When our server is started, we can see this API endpoint when we use to see our data come back in JSON form.

当服务器启动时,当我们使用查看数据以JSON形式返回时,我们可以看到此API端点。

结论 (Conclusion)

Hopefully this package will help you in your JavaScript development. This package helps to easily mock an API and return user information. You can get creative with it and even use this package for financial transactions.

希望此软件包将对您JavaScript开发有所帮助。 该软件包有助于轻松模拟API并返回用户信息。 您可以发挥创意,甚至可以将此软件包用于财务交易。

I would encourage you to take a look at the as well which helps you visualize the amount of data that comes out of this package.

我鼓励您也去看看 ,它可以帮助您可视化此程序包中的数据量。

翻译自:

faker 测试数据生成

转载地址:http://inywd.baihongyu.com/

你可能感兴趣的文章
应用程序缓存的应用(摘抄)
查看>>
jQuery基础知识,很赞的!!!
查看>>
JK_Rush关于索引的一些总结
查看>>
[Codevs] 线段树练习5
查看>>
Amazon
查看>>
component-based scene model
查看>>
Echart输出图形
查看>>
hMailServer搭建简单邮件系统
查看>>
从零开始学习jQuery
查看>>
Spring+SpringMVC+MyBatis深入学习及搭建(四)——MyBatis输入映射与输出映射
查看>>
opacity半透明兼容ie8。。。。ie8半透明
查看>>
CDOJ_24 八球胜负
查看>>
Alpha 冲刺 (7/10)
查看>>
一款jQuery打造的具有多功能切换的幻灯片特效
查看>>
SNMP从入门到开发:进阶篇
查看>>
@ServletComponentScan ,@ComponentScan,@Configuration 解析
查看>>
unity3d 射弹基础案例代码分析
查看>>
thinksns 分页数据
查看>>
os模块
查看>>
C# 上传文件到指定路径
查看>>