There are many times, where customers want to see how their projects are working, how they are progressing and faker can be a solution to give them what they want, it is also essential for testing purposes.
A few weeks ago, a client asked me to build a peer to peer platform, once everything was up and running, but something was missing, not least from the users.
As a developer I couldn’t make sure I could find a myriad of real ones in a day. So instead of spending hours creating fake accounts or populating the database directly, I decided to create one.
Faker appeared to me, as the solution, 20min, and 2 scripts later the site had its real fake users.
Faker is a gem that generates fake data.
It comes in very handy for having real-looking test data, and having your database populated with more than one or two records while you’re doing development.
Here is my script to populate my user, category and request table.
Enjoy
10.times do
Category.create(
name: Faker::Job.unique.field
)
end
# Create dummy users
5.times do
user = User.create(
full_name: Faker::Name.name,
email: Faker::Internet.free_email,
about: Faker::Quote.matz,
password: '123456',
from: Faker::Address.country,
language: Faker::Nation.language,
created_at: Date.today
)
user.avatar.attach(
io: image = open("https://i.pravatar.cc/300"),
filename: "avatar#{user.id}.jpg",
content_type: 'image/jpg'
)
end
# Create dummy Requests
10.times do
random_user = User.all.sample(1)[0]
category = Category.all.sample(1)[0]
request = Request.create(
title: Faker::Job.title,
description: Faker::Quote.matz,
budget: Faker::Number.between(5, 50),
delivery: Faker::Number.between(1, 10),
user_id: random_user.id,
category_id: category.id
)
end