ppascualv
ppascualv

Reputation: 1137

Rails Faker how to create custom method

I'm using rspec and faker for testing and I want to add a cuestom field on faker, I had followed this instructions:

https://github.com/stympy/faker#customization

So in my rails_helper.rb I have this line:

Faker::Config.locale = :ca

And in my ca.yml under config/locales folder I have:

faker:
    internet:
      usefuldata: [AAAAA,BBBBB]

And when I made Faker::Internet.usefuldata it returns undefined method 'usefuldata' for Faker::Internet:Class. And I want that Faker::Internet.usefuldata return AAAAA or BBBBB.

Thanks in advance.

Upvotes: 2

Views: 2599

Answers (1)

Prakash Murthy
Prakash Murthy

Reputation: 13067

Faker cannot currently support customization of Internet data.

Faker supports customization only for those classes that contain the flexible method in their definition. Eg: The Faker::Name class has the flexible method

For customizing name, have the locale file - for example, fr.yml - in the following format:

fr:
  faker:
    name:
      region: [South East Queensland, Wide Bay Burnett, Margaret River, Port Pirie, Gippsland, Elizabeth, Barossa]

Here, region is a custom field being added to name.

Note: The first level is fr, not faker.

Other classes that support customization:

  • Faker::Address
  • Faker::Business
  • Faker::Company
  • Faker::Hacker
  • Faker::Team

I have forked the repo and added the ability to customize internet fields. You can try it out by setting the source appropriately in the Gemfile as follows:

gem 'faker', git: 'https://github.com/prakashmurthy/faker.git'

Not too keen on getting this merged into faker gem as useful_data doesn't seem to be a common enough data point for a lot of users.

Upvotes: 4

Related Questions