Using Faker with Rails
A guide to setting up Faker to work within Rails and why you'd want to
Faker is a Gem that helps you to generate fake data within a Ruby based application.
We like using Faker here at Tosbourn Ltd as it allows us to generate fake data quickly and easily. It helps to minimise file sizes as you’re not having to type out multiple characters if you’re faking something like paragraphs of content. It also helps to relieve the mental headache of trying to think of fake data.
Getting Faker set up
Getting Faker to work with your Rails app is straightforward, we have it in the main part of our application’s Gemfile as we use it in several places (seeds and tests), if you plan on just using it within tests then place it in the
test block of your Gemfile. Here’s how we do it:
Gemfile gem 'faker'
You will then need to run
bundle install to get the dependencies into your project. Faker will then be globally available to use within your project.
Basic usage of Faker
Here’s an example of basic usage of Faker:
Faker::Internet.email # generates random fake email address i.e. email@example.com
Faker provides various Generators that you can use to generate data within your app. From the useful Name Generator which can provide all sorts of fake name data like
prefix to the less useful but more fun Lebowski Generator which can provide
quote fake data from the Big Lebowski. If you’re thoughtful with how you use the generators it can help to explain what your attribute is supposed to be. So for example if you were faking out an article
body if you used
Faker::Lorem.paragraphs, it makes it more obvious to someone reading your code that an article body is expected to have paragraphs.
Faker is great to use within RSpec to fake out data and keep your tests looking more streamline. So instead of something like this:
spec/system/article_spec.rb RSpec.describe 'Articles', type: :system do scenario 'Article title is displayed on home page' do article = create(:article, title: 'The title of my article') # We use FactoryBot factories to generate test objects visit root_path expect(page).to have content(article.title) end end
You could have this:
spec/system/article_spec.rb RSpec.describe 'Articles', type: :system do scenario 'Article title is displayed on home page' do article = create(:article, title: Faker::Lorem.sentence) # We use FactoryBot factories to generate test objects visit root_path expect(page).to have content(article.title) end end
Whilst this is a bit of a contrived example this could come in useful if you had an attribute which has a length validation, say for an article body which requires a length of at least 200 characters. You don’t want to have a test where you have to have a big block of text, in this scenario you could use Faker like this:
spec/system/article_spec.rb RSpec.describe 'Articles', type: :system do scenario 'Article has a body' do article = create(:article, body: Faker::Lorem.paragraph_by_chars(number: 210)) visit article_path(article) expect(page).to have content(article.body) end end
Unique values with Faker
If you have a model attribute which has a uniqueness validation Faker has a
unique method you can make use of:
Everytime this is called it will bring back a unique value. This is useful if you are creating numerous objects with unique attributes with tests i.e. if you’re using a
create_list with FactoryBot:
create_list(:article, 5, title: Faker::Lorem.unique.sentence) # this will create 5 articles with unique titles
Using Faker in seeds.rb
We also use Faker within our
seeds.rb file to fake out data and to keep it more streamlined. Usage is the same as within RSpec and can be used if you’ve allowed it to be accessed globally within your Gemfile.
db/seeds.rb Article.create(title: Faker::Lorem.unique.sentence) do |article| article.body = Faker::Lorem.paragraphs(number: 4) end
You can create realistic looking data if you think about what generators you use with your seeds, for example the Address generator allows us to create realistic looking addresses without having to think of fake data:
db/seeds.rb Address.create(address_line_1: Faker::Address.street_address) do |address| address.city = Faker::Address.city address.postal_code = Faker::Address.zip address.country = Faker::Address.country end
Which will give us something like this:
8197 Kiehn Hills South Horace 17606 Rwanda
This is without ever having to think of the data ourselves and having something which looks realistic in our development environment. You could build out your entire seeds file like this with realistic looking data.