DEV Community

Cover image for The Convenience of Faker
Daniel Joo
Daniel Joo

Posted on

The Convenience of Faker

When working on a new project, everyone needs to populate their databases with some data. This can be a daunting task, especially for beginners to programming. In Ruby however, there is a "Faker" gem, which, upon installing, can make that task a whole lot easier. There is pretty much no need to hardcode any fake data when you need a lot of data!

What Faker does is generates fake data for your database. Before you use Faker, three things need to be done. First, you have to install the gem with "gem install faker." Second, you must set up made a migration for your tables and have your schema ready. This can be done through Active Record. Lastly, you need to require Faker on your gemfile. Now, you are all set to use Faker to generate this fake data!

Faker is such a convenient tool, and although I won't get into the specifics of how to actually incorporate it into the code, everyone who works with Ruby on their personal projects should utilize it.

Top comments (1)

Collapse
 
kwstannard profile image
Kelly Stannard

There is an anti-pattern with Faker that I would like to spread awareness of.

Don't use Faker in your tests. It might seem like a fun way to avoid uniqueness constraints, but eventually Faker will repeat itself and fail a test. I have probably had to remove Faker calls a half dozen times due to this. Use an infinite sequence in tests instead.

Faker is great for populating an MVP or prototype though.