Gem of the Week – Faker
Use the Faker Gem to Make High Quality, High Volume Fake Data for Testing and Development

Faker Gem

In almost every system we build, the first version of a feature involves getting the functional specs right, and the second version usually involves some kind of optimization.  You might build a dashboard and create 3 or 4 records by hand to make sure everything works.  But when it comes time to test and optimize, you need a few dozen, or perhaps a few hundred or thousand records.  This is where Faker comes in.

Faker makes it really easy to generate fake data.  You simply install the gem by adding it to your Gemfile (note that I’ve only added it to the development group:


group :development do

...
  gem 'faker'

...
end

Faker supports a ton of patterns out of the box, and is easy to understand, thanks to the really well written Readme file.  You can also customize it if the patterns don’t meet your needs.

To use Faker, in your seeds.rb file (or wherever else you need data, dynamic fixtures, for example) simply reference the faker objects as rvalues:


100.times do

Person.create({
  first_name: Faker::Name.first_name,
  last_name: Faker::Name.last_name,
  email: Faker::Internet.email
})

end

Another nice benefit is that there is support for a lot of languages, and you can configure your locale with a single line:

Faker::Config.locale = 'en-US'

This is a nice feature because of different phone number formats, and different names. You can see a list of supported locales here.

Thanks to Benjamin Curtis for maintaining such a high-value, low-effort gem.