0

Here's my Ride factory:

FactoryGirl.define do
  factory :ride do
    departure_address "#{Faker::Address.street_address}, #{Faker::Address.city} #{Faker::Address.country}"
  end
end

How to clean up this ugly code? "#{Faker::Address.street_address}, #{Faker::Address.city} #{Faker::Address.country}"

I'd like to create a fake_full_address method inside test/helpers and call it from the factory. How to do it?

Webspirit
  • 1,013
  • 3
  • 12
  • 18

1 Answers1

0

Under /lib you can put your helper module:

module MyHelper
  def self.full_address
    "#{Faker::Address.street_address}, #{Faker::Address.city} #{Faker::Address.country}"
  end
end

The in your factory you can us it including your helper:

require 'my_helper.rb'

FactoryGirl.define do
  factory :ride do
    departure_address MyHelper.full_address
  end
end

I did this on my project and it works well, but you can check these options too...

How to include a module in a factory_girl factory?

https://github.com/thoughtbot/factory_girl/issues/564

Community
  • 1
  • 1
Leantraxxx
  • 4,506
  • 3
  • 38
  • 56