10

Is there a way to get the Faker gem to generate 'correlated' city and country code values?

For example,

  • Vancouver, CA
  • Minneapolis, MN

I'm doing this:

FactoryGirl.define do
  factory :location do
    ...
    city {Faker::Address.city}
    country_code {['US', 'CA'].sample}
    ...
  end
end

But there is no guarantee that the city will actual reside in country_code.

I'd settle for something like:

postal_code {Faker::Address.postcode(['US', 'CA'].sample) }

Which I could then geocode to get the other values.

craig
  • 25,664
  • 27
  • 119
  • 205

1 Answers1

4

You can customize based on locales: https://github.com/stympy/faker#customization

So you can create or modify a locale file, see this example: https://github.com/stympy/faker/blob/master/lib/locales/en-NEP.yml

Then you can set what the default country should be and also what states do you want to use

Aguardientico
  • 7,641
  • 1
  • 33
  • 33
  • on a side note, im wondering if the faker gem is reliable in terms of not breaking your tests (e.g: the generated code text duplicates and stuff like that) – dtc May 14 '15 at 08:18