

Generate unique data with Fzaninotto/Faker
source link: https://barryvanveen.nl/articles/30-generate-unique-data-with-fzaninottofaker
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Generate unique data with Fzaninotto/Faker
Jul 26, 2016
Fzaninotto/Faker is a package for generating all sorts of random data. This tip shows you how you can use it to generate unique data for usage in tests.
What is Faker?
With the help of Faker, it is possible to generate random data. Not only random numbers and Lorem ipsum. What makes Faker so great is that you can make (localized) names, telephone numbers, email addresses, postal codes and much much more.
Have a look at the GitHub page to see the whole repetoire.
Using Faker in your tests
This is really great, but when you use it in tests you will run into some problems. Consider the following scenario:
You want to generate 3 comments that should be visible on your blog. Then you generate 3 different comments that should not be visible to users. You verify this using the see()
method that is available by default in Laravel 5.2
$blog = factory(Barryvanveen\Blogs\Blog::class, 'published')->create();
$online_comments = factory(Barryvanveen\Comments\Comment::class,
'online', 3)->create(['blog_id' => $blog->id]);
$offline_comments = factory(Barryvanveen\Comments\Comment::class,
'offline', 3)->create(['blog_id' => $blog->id]);
$this->visit(route('blog-item', ['id' => $blog->id, 'slug' => $blog->slug]))
->see(trans('comments.title'));
foreach ($online_comments as $comment) {
$this->see($comment->text);
}
foreach ($offline_comments as $comment) {
$this->dontSee($comment->text);
}
This setup might lead to problems if you don't configure the ModelFactory in the correct way. The text
property for a comment could be generated by $faker->paragraph
. But, this does not guarantee that each text is unique, so once in a while, your test might fail because a comment that is online and a comment that is offline both have the same text.
To counter this we can use Faker's unique()
method
Faker's unique method
The unique()
method is a chainable method that can be used to make any format (ie. any type of data) unique. This is done by keeping a list of generated data and re-generating new data until a unique value is found. A maximum number of retries is used to prevent never-ending loops.
Usage it like so:
$faker->unique()->paragraph;
$faker->unique()->name;
$faker->unique()->randomNumber(5);
It is no surprise that you cannot generate more than 2 unique Boolean values, so use this method with care. In much the same way it is very hard to generate 10.000 unique sentences, so don't push it.
More articles
Recommend
-
162
Faker Faker is a PHP library that generates fake data for you. Whether you need to bootstrap your database, create good-looking XML documents, fill-in your persistence to stress test it, or anonymize data taken from a production service,...
-
91
README.md Java Faker
-
9
BackendRails profiling story, or how I caught Faker trying to teach my app Australian SlangFebruary 12, 2019 Once upon a time (actually, a few days ago) I’ve been working on a small refactoring task: integr...
-
6
Fake data can be invaluable for testing or demonstrating behaviour without using live, production data. This lets you protect your production data or help you get started when you don’t yet have a production system set up. This post gives an...
-
4
faker:我都是自愿的,我喜欢159.7万播放 · 1359弹幕2021-05-27 06:24:35 全站排行榜最高第31名 faker:我都是自愿的,我喜欢...
-
6
大司马三烧faker那局有没有可能不是本人打的?116 个回答大...
-
4
Faker is a Python package that generates fake data for you. Whether you need to bootstrap your database, create good-looking XML documents, fill-in your persistence to stress test it, or anonymize data taken from a production service...
-
16
Faker Generate massive amounts of fake data in the browser and node.js. FAQ - What happened to the original faker.js? This project was originally created and hosted at http...
-
4
Generating fake security data with Python and faker-securityMichael AquilinaApril 25, 2022
-
4
Laravel 8 Generate Fake Data Using Faker Example 90228 views 1 year ago Laravel Sometimes you are testing project i...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK