3 Ways To Build Fake Demo Data For Your Rails App
You've finished the first sprint of your new Rails application and you're ready to show it to the client - or are you? Most user interfaces look better when they're actually filled with data. You could sit down and start entering records by hand, wracking your brain to think of new names and addresses. Or you could take the easier way out by turning to a random data generation library.
We previously looked at the Faker library from Ben Curtis. Now at version 0.3.1, Faker is a port of Data::Faker from perl. Faker includes modules to generate random names, addresses, phone numbers, and more. Faker's data is hard-wired as lookup arrays in its source code, though, and it's installed as a gem, making it somewhat difficult to extend. Two other data generators are easier to work with in this regard.
The first of these is the random-data gem, currently at version 1.5.0. The brainchild of Mike Subelsky, random-data has a somewhat wider range than faker, covering names, dates, text, addresses, phone numbers, and more. As with Faker, the basic building blocks of the data are hardwired in the source code, but random-data also implements
method_missing in a useful way: if you have a file named "spam.dat" in your load path, then calling
Random.spam will return random lines from the file.
Rails developers will also want to check out Nate Sutton's forgery plugin. Forgery uses a Rails generator to move its data into your application, and it also separates data dictionaries from the actual generation code, making it extremely simple to change things like the list of names or cities that it includes. Forgery also has more data to choose from than the other two libraries, leading to an increased appearance of randomness in the end product.
Is this the end of the test data generation story? Probably not. At least random-data and forgery are still under active development, and I can think of some useful ways to extend them. For example, it would be great to see some sort of correlated generator that would ensure that a state, zip code, and area code all matched up roughly with one another. But even in their current state, any of these libraries will do better to fill out your test user interface than laboriously putting in everything by hand.
Tip: There's a more basic guide on generating fake names, addresses and more in Ruby over at Ruby And How!