Img caption: railscarma.com

Developers may feel they do not have that much creativity when it comes to use random words as arguments, or values for many fields. For example, if we have some test cases when we are creating a new instance of a Book object, chances are that we will assign ‘book’ to this object’s title, which does not seem that much interesting. We code because it is something that gives us pleasure, and getting help with these types of difficulties can be helpful. Fortunately, there is a Ruby gem called Faker that generates random fake value that you can use in your tests.

When we want to assign different values to many testing objects’ attributes, but simply find it insane to come up with that much meaningful values, Faker gem becomes very valuable.

For the time being Faker has many classes for values of different scenarios. If you need to set a user’s address, then you can type Faker::Address.street_address. If you need to set a user’s name, you can simple use Faker::Name.name. If you need omniauth data from Google, then you can type Faker::Omniauth.google. If you want to have unique names, then you can use: Faker::Name.unique.name.

It is very easy to install this gem. All you have to do is execute the following:

gem install faker

or import it in your Gemfile and then execute: bundle in your Rails project.

Then if you want to use it in plain Ruby projects, you need first to import it:

require ‘faker’

You can learn more about it from the Github repository of the gem. In this repository you can also see the implementation, and contribute with your own type of examples that you want to see them included in this gem.