ffaker Ruby Library

repository·main·Indexed 23 days ago

https://github.com/ffaker/ffaker

A Ruby library for generating realistic fake data for testing and development, such as names and emails. A rewrite and fork of the 'faker' gem, ffaker is optimized for speed and includes specialized modules for localized address generation (including BE, BR, DA, FI, GR, IN, JA, KR, and US), unique value generation, and deterministic data via FFaker::Random.

Tokens
18.6K
Snippets
17
Records
185
Agent score
80%

What's inside ffaker

  1. How ffaker handles missing constants

    main

    The ffaker library overrides const_missing for its modules. If you attempt to access a constant that is not explicitly defined in a module, ffaker will attempt to find a matching data file in the ffaker/data directory.

    For example, accessing FFaker::Name::FIRST_NAMES for the first time will trigger the library to look for and load data from ffaker/data/name/first_names and set that constant automatically.

  2. Implement deterministic ffaker modules

    main

    When building new ffaker modules, you must use the deterministic methods provided by ModuleUtils to ensure the output respects the random seed.

    Instead of using standard Ruby Array methods, use these equivalents:

    • fetch_sample(array) instead of Array#sample.
    • fetch_sample(array, count: n) instead of Array#sample(n).
    • shuffle(array) instead of Array#shuffle.

    Note that standard calls to rand will automatically use the correct internal RNG without modification. If you need to pass the RNG to other Ruby methods, you can access it via FFaker::Random.

  3. Sync FFaker seed with RSpec

    main

    To make ffaker deterministic with RSpec (including seeds passed via --seed nnn), add the following configuration to your spec_helper.rb or equivalent file:

    • Use config.before(:all) to set the seed based on the RSpec seed.
    • Use config.before(:each) to reset the RNG so each test is independent.
    # spec_helper.rb
    RSpec.configure do |config|
      config.before(:all)  { FFaker::Random.seed=config.seed }
      config.before(:each) { FFaker::Random.reset! }
    end
  4. Test repeatability of ffaker modules

    main

    To ensure your ffaker modules are deterministic, use the DeterministicHelper in your tests.

    1. Include the helper: include DeterministicHelper in your test class.
    2. Test multiple methods: Use assert_methods_are_deterministic(Class, :method1, :method2) for methods that do not require arguments.
    3. Test specific blocks: Use assert_deterministic { ... } for methods that require arguments or have complex behavior.
    # Example usage in a test class
    class MyModuleTest < Minitest::Test
      include DeterministicHelper
    
      def test_methods_without_args
        assert_methods_are_deterministic(
          FFaker::NewFFakerModule,
          :method_name, :other_method_name, :another_method_name
        )
      end
    
      def test_some_method
        assert_deterministic { FFaker::NewFFakerModule.some_method(:required_argument) }
      end
    end
  5. Sync FFaker seed with Minitest

    main

    To ensure ffaker returns the same data every time a specific Minitest seed is used (e.g., via --seed nnn), follow these two steps:

    1. Create a Minitest plugin: Create test/minitest/ffaker_random_seed_plugin.rb with the following content:
    module Minitest
      def self.plugin_ffaker_random_seed_init(options)
        FFaker::Random.seed = options[:seed]
      end
    end
    1. Reset seed in test setup: In your test_helper.rb or your test case superclass, add a before_setup method that calls FFaker::Random.reset! to ensure each test starts from a predictable state.
    # test/minitest/ffaker_random_seed_plugin.rb
    module Minitest
      def self.plugin_ffaker_random_seed_init(options)
        FFaker::Random.seed = options[:seed]
      end
    end
    
    # test_helper.rb or similar.
    class TestBase < Minitest::Test
      def before_setup
        FFaker::Random.reset!
      end
    end
    
    class TestSomethingUsingFFaker < TestBase
      def test_something_using_ffaker
        # use FFaker as normal
      end
    end
  6. Configure ffaker for Rails

    main

    If you are using Ruby on Rails, add ffaker to your Gemfile within the development and test groups to ensure it is available during local development and automated testing:

    group :development, :test do
      gem 'ffaker'
    end

    After updating your Gemfile, run the following command in your terminal:

    bundle install
  7. Basic usage of ffaker

    main

    Once required, you can access various data generators through the FFaker module. Common modules include Name and Internet.

    require 'ffaker'
    
    FFaker::Name.name       #=> "Christophe Bartell"
    FFaker::Internet.email  #=> "kirsten.greenholt@corkeryfisher.info"
  8. Generate Russian gender values with FFaker::GenderRU

    main

    Use the FFaker::GenderRU module to generate gender-related strings in Russian.

    • maybe: Returns a random selection from common gender identities (e.g., 'мужчина', 'женщина', 'агендер').
    • random: Returns a random selection.
    • sample: Returns a random selection from a sample set.
    • binary: Attempts to return a binary gender value. Note that this method may raise FFaker::UniqueUtils::RetryLimitExceeded if it cannot find a unique value within the retry limit.