Tune your factory

As a Rails developer you’ve surely seen factory like this:

FactoryGirl.define do
  factory :event do
    sequence(:id, 1000)
    sequence(:name) {|n| "Event #{n}" }
    description 'Lorem ipsum Elit proident Excepteur commodo aliqua ut tempor laborum sunt labore incididunt qui.'
    starts_at rand(3.days.ago..Time.now)
    stops_at rand(Time.now..3.days.from_now)
    lat rand(-90.0..90.0)
    long rand(-180.0..180.0)
    country_code 'IT'
    city 'Milan'
    address 'Via Venini 42'
    published false
  end
end

Faker is a gem that allow you to generate data. With Faker you can build a large amount of different kind of data with a simple DSL, saving you al lot of time. The same factroy rewritten using Faker would look like this.

FactoryGirl.define do
  factory :event do
    sequence(:id, 1000)
    sequence(:name) {|n| "Event #{n}" }
    description Faker::Lorem.paragraph
    starts_at Faker::Date.between(3.days.ago, Date.today)
    stops_at Faker::Date.forward(3)
    lat Faker::Address.latitude
    long Faker::Address.longitude
    country_code Faker::Address.country_code
    citi Faker::Address.city
    address Faker::Address.street_address
    published false
  end
end

To use Faker in your rails app simply add gem 'faker' in your gemfile.

Happy building!

Leave a Reply

wpDiscuz