Faker

repository·dev·Indexed 22 days ago

https://github.com/elixirs/faker

A pure Elixir library for generating fake data, primarily used for testing purposes. It provides modules for generating localized addresses, application versions, beer brands, commerce data, file extensions, and various codes like ISBN, ISSN, and IBAN. Supports OTP 19+ and Elixir 1.6+.

Tokens
5.3K
Snippets
48
Records
53
Agent score
78%

What's inside Faker

  1. Quickstart: Install and set up Faker

    dev

    To use Faker in your Elixir project, add it to your dependencies and initialize it in your test helper. It is recommended to restrict Faker to the :test environment to avoid unnecessary overhead in production.

    1. Add {:faker, "~> 0.19.0-alpha.5"} to your mix.exs file.
    2. Run mix deps.get to fetch the dependency.
    3. Call Faker.start() in your test/test_helper.exs to ensure the library is properly initialized before running tests.
    # In mix.exs
    defp deps do
      [
        {:faker, "~> 0.19.0-alpha.5", only: :test}
      ]
    end
    
    # In test/test_helper.exs
    ExUnit.start()
    Faker.start()
  2. Troubleshoot Faker.start() errors

    dev

    If you encounter a FunctionClauseError similar to no function clause matching in Faker.Address.city_count/1 when calling Faker modules (e.g., Faker.Address.city/0), it means the library has not been initialized.

    To fix this, ensure you have added Faker.start() to your test/test_helper.exs and that :faker is included in your application's applications list in mix.exs.

  3. Generate vehicle make and model data

    dev

    Use Faker.Vehicle to generate various vehicle-related strings including makes, models, and combined make/model strings.

    • make(): Returns a vehicle manufacturer (e.g., "Lincoln").
    • model(): Returns a vehicle model (e.g., "Encore").
    • model(make): Returns a model string that belongs to the specific provided make (e.g., Faker.Vehicle.model("Ford") might return "Focus").
    • make_and_model(): Returns a combined string of a make and its model (e.g., "Chevrolet Malibu").
    Faker.Vehicle.make()
    Faker.Vehicle.model("Ford")
    Faker.Vehicle.make_and_model()
  4. Generate IBAN codes with Faker.Code

    dev

    Use Faker.Code.iban/1 or Faker.Code.iban/2 to generate a random International Bank Account Number (IBAN).

    Note: The provided components (country code or prefixes) are included in the checksum calculation but are not validated by the generator.

    # Generate a random IBAN with a single country code
    Faker.Code.iban("NL")
    
    # Generate a random IBAN with a country code and prefix components
    Faker.Code.iban("NL", ["ABNA"])
    
    # Generate a random IBAN with a country code and multiple prefix components
    Faker.Code.iban("MC", ["FOO", "BAR"])
  5. Generate slugs with Faker.Internet

    dev

    Faker.Internet.slug/0 generates a URL-friendly slug.

    • slug/0: Generates a slug using 2 to 5 random words joined by a random glue (one of -, _, or .).
    • slug(words): Generates a slug from a provided list of strings using default glue.
    • slug(words, glue): Generates a slug from a provided list of strings using a specific list of glue characters.
    Faker.Internet.slug()
    # => "deleniti-sint-consequatur-ut"
    
    Faker.Internet.slug(["foo", "bar"])
    # => "foo_bar"
    
    Faker.Internet.slug(["foo", "bar"], ["."])
    # => "foo.bar"
  6. Generate URLs and image URLs with Faker.Internet

    dev
    • url/0: Returns a random URL with either http:// or https:// prefixes.
    • image_url/0: Returns a random image URL from services like placekitten.com, picsum.photos, or dummyimage.com.
    Faker.Internet.url()
    # => "https://sipes.com"
    
    Faker.Internet.image_url()
    # => "https://placekitten.com/131/131"
  7. Generate geographic coordinates and geohashes

    dev

    Generate precise geographic data:

    • latitude(): Returns a random latitude as a float.
    • longitude(): Returns a random longitude as a float.
    • geohash(): Returns a random geohash string based on generated latitude and longitude.
    Faker.Address.geohash()
    # Example: "1kgw0"
  8. Generate random file extensions with Faker.File.file_extension/0 and Faker.File.file_extension/1

    dev

    Use Faker.File.file_extension/0 to get a random file extension from any available category. Use Faker.File.file_extension/1 to restrict the result to a specific category.

    Available categories: :audio, :image, :text, :video, :office.

    # Random extension
    Faker.File.file_extension()
    
    # Extension from a specific category
    Faker.File.file_extension(:video)
  9. Generate network addresses (IPv4, IPv6, MAC) with Faker.Internet

    dev

    Use these functions to generate network-related identifiers:

    • ip_v4_address/0: Generates a random IPv4 address.
    • ip_v6_address/0: Generates a random IPv6 address.
    • mac_address/0: Generates a random MAC address.
    Faker.Internet.ip_v4_address()
    # => "214.217.139.136"
    
    Faker.Internet.ip_v6_address()
    # => "A2D6:F5D9:BD8B:C588:0DC8:9566:43F4:B196"
    
    Faker.Internet.mac_address()
    # => "d6:d9:8b:88:c8:66"
  10. Generate commerce and pricing data with Faker.Commerce

    dev

    The Faker.Commerce module provides functions for generating random commerce-related data such as colors, departments, prices, and product details.

    iex> Faker.Commerce.color()
          "red"
    
    iex> Faker.Commerce.department()
          "Electronics & Computers"
    
    iex> Faker.Commerce.price()
          1.11
    
    iex> Faker.Commerce.product_name()
          "Ergonomic Steel Shirt"
  11. Generate domain names and suffixes with Faker.Internet

    dev

    Use Faker.Internet.domain_name/0 to generate a complete random domain name (e.g., blick.org). You can also generate individual components using Faker.Internet.domain_word/0 for the name part and Faker.Internet.domain_suffix/0 for the TLD (e.g., com, org).

    Faker.Internet.domain_name()
    # => "blick.org"
    
    Faker.Internet.domain_word()
    # => "blick"
    
    Faker.Internet.domain_suffix()
    # => "org"