Faker – names, addresses and emails at the push of a button
If you test a program – a form, a shop, the time tracker from the advanced course – you need data: names, addresses, email addresses, phone numbers. Typing “John Doe” a hundred times is dull, and real customers' data does not belong in tests. The Faker gem makes it up: it has dictionaries of first names, streets, cities, dishes, companies and much more, and puts together what looks real.
install_gem "faker"
require "faker"
puts Faker::Name.name # the first time takes a few seconds
puts Faker::Internet.email
puts Faker::Address.city
puts Faker::Food.dish
Faker::Lorem.sentenceEach class is a topic: Faker::Name, Faker::Internet, Faker::Address, Faker::Food, Faker::Lorem (filler text) – there are over 200. Run the cell again: something different comes out every time.
And why does the first call take so long? That is when Faker reads its dictionaries: 318 YAML files, 4.6 MB, for over 60 languages. On your computer that is done in a moment; here in the browser it takes a few seconds – once, then it is quick. (A live run while you type does not read them; that needs ▶.)
Random is good, but sometimes awkward for tests: if a test fails with one particular name, you want to see exactly that name again. So Faker's randomness can be pinned down:
Faker::Config.random = Random.new(42)
first = 3.times.map { Faker::Name.first_name }
Faker::Config.random = Random.new(42)
again = 3.times.map { Faker::Name.first_name }
[first, again, first == again]Random.new(42) is a random number generator with the seed 42. The same seed always gives the same sequence of “random” numbers – and so the same names. A test that starts like this sees the same data on every run.
Faker speaks many languages. Faker::Config.locale picks one – here Australian English, with Australian addresses and phone numbers, and Japanese:
Faker::Config.locale = "en-AU"
puts Faker::Name.name
puts Faker::Address.full_address
puts Faker::PhoneNumber.phone_number
Faker::Config.locale = "ja"
puts Faker::Name.name
puts Faker::Address.city
Faker::Config.locale = "en"The language holds for every later call, so at the end we switch back to English. Where a language lacks a dictionary, Faker uses the English one. Try "de-CH" for Swiss German or "fr" too.
Sometimes nothing may repeat – customer numbers, say, or email addresses that must be unique. That is what unique is for:
Faker::Number.unique.clear
dice = 6.times.map { Faker::Number.unique.between(from: 1, to: 6) }unique remembers what came out and never gives the same thing twice: six throws, each number once. A seventh throw would not work – after a few tries Faker gives up with Faker::UniqueGenerator::RetryLimitExceeded. clear forgets the remembered values; without the first line the cell fails when you run it a second time.
And this is what test data for the time tracker from the advanced course looks like – with a fixed seed, so every run has the same entries:
Faker::Config.random = Random.new(7)
entries = 4.times.map do
{
project: Faker::App.name,
person: Faker::Name.first_name,
hours: Faker::Number.between(from: 1, to: 8)
}
end
entries.each do |entry|
puts "#{entry[:person].ljust(10)} #{entry[:project].ljust(12)} #{entry[:hours]} h"
end
entries.sum { |entry| entry[:hours] }gem install faker, or in the Gemfile in the :test group. In Minitest tests (lesson 111) Minitest's own seed is a good choice; every run prints it (Run options: --seed 12345):
Faker::Config.random = Random.new(Minitest.seed)
Then ruby test.rb --seed 12345 makes exactly the data of the failed run again. Faker::Config.random = nil makes randomness random again.
Random.new(2024) and make an array customers of 5 hashes, each with a :name and an :email.install_gem "faker"
require "faker"
# 5 test customers with :name and :email, with the seed 2024