Seed data made easy with Faker in Ruby

Peter Crawford
4 min readMay 11, 2021

Dummy data is an essential part of all testing, but especially database query testing. The tool I’ve been exposed to that makes this process relatively simple is Faker.

I recently had to create dummy test data for a code challenge and quickly hunting around the web I found that there wasn’t a single, easy, yet comprehensive page to help me setup dummy data quickly. So I decided to create a blog entry to help you setup your dummy test data, whether or not you’re under the time crunch of a code challenge.

Installation

First we need to add the Faker gem to the Gemfile in our application.

As an alternative we may also run gem install faker from the terminal

In our configuration.rb file you should have the require “faker” entry. In some places it is suggested that this entry be put in the seeds.rb file.

Once we’ve done these simple but important steps we’re now ready to setup our dummy test data

Data Setup

Now we move to our seeds.rb data routine file to setup our dummy test data. Please note that it is necessary for us to have setup our data structures as well as our class macros by this point.

First it is prudent to have code to first remove any existing data in our database before installing a full set of new data. This will avoid duplicate or corrupted entries in your dummy dataset.

Now the dummy training data for the Reader data table can be created with the help of the Faker utility.

In this first example the name and email data fields are generated on each iteration through the 10.times the data creation loop is run. See the seed data result in the Seed Data section below.

Example 1 — Data Creation

In the second example the salary data value is randomly generated between $1,000 and $1,000,000 and the character name value is created from the character of the movie The Big Lebowski.

Example 2 — Data Creation

Seed Data

To run our routine and seed your dummy test data into our database we’ll need to run the ruby db/seeds.rb command in the terminal

If we have we have the rake facility then the rake db:seed command can be run in the terminal instead.

To check that our dummy test data population has worked as expected we should enter the terminal console, in this example with the use of rake console. We should now run a quick query on the data:

Query the Reader data file contents

The data is generated with Faker name and email values, as below

Example 1 — Data Created

If the data does not appear as expected we should check our seeds.rb file and ensure we’ve formed it correctly before running the seeds routine again. The .destroy_all statements that we put in at the top of the seeds.rb file will ensure that any corrupted data is wiped away before repopulation.

Conclusion

Dummy data creation techniques are a very important part of the Ruby developer toolkit and Faker is an invaluable asset within that.

Please see below some useful links from the sources I had to cobble together during my code challenge. Hopefully this blog entry will save you a little hunting around.

Useful Links:

https://github.com/faker-ruby/faker#generators

https://rubydoc.info/github/faker-ruby/faker/master/Faker

https://medium.com/@mariorodriguezan/make-your-life-easier-and-install-faker-ruby-gem-6dc491445d5

--

--