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 Marak has created a neat package that lets us create fake data on the fly: faker.
幸运的是, Marak创建了一个简洁的程序包,可让我们即时创建虚假数据: faker 。
一个快速的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.
让我们进一步了解如何使用此有用的软件包。