项目作者: faker-ruby

项目描述 :
A library for generating fake data such as names, addresses, and phone numbers.
高级语言: Ruby
项目地址: git://github.com/faker-ruby/faker.git
创建时间: 2008-12-24T17:20:37Z
项目社区:https://github.com/faker-ruby/faker

开源协议:MIT License

下载


logotype a happy-07

Faker

Tests
Gem Version
Inline docs

Faker is a port of Perl’s Data::Faker library.
It’s a library for generating fake data such as names, addresses, and phone numbers.

Faker helps you generate realistic test data, and populate your
database with more than a couple of records while you’re doing development.

It comes in very handy for taking screenshots (taking screenshots for a personal project)
and it was the original impetus for the creation of this gem.

In the media

Table of Contents

Notes

  • While Faker generates data at random, returned values are not guaranteed to be unique by default.
    To explicitly specify when you require unique values, see Ensuring Unique Values.
    Values also can be deterministic if you use the deterministic feature, see Deterministic Random
  • This is the main branch of Faker and may contain changes that are not yet released.
    Please refer to the README of your version for the available methods.
    The list of all versions is available here.

Getting Started

Start by including faker in your Gemfile:

  1. gem 'faker'

Then run bundle install.

Usage

Here are some examples of how to use Faker:

  1. require 'faker'
  2. Faker::Name.name #=> "Christophe Bartell"
  3. Faker::Address.full_address #=> "5479 William Way, East Sonnyhaven, LA 63637"
  4. Faker::Markdown.emphasis #=> "Quo qui aperiam. Amet corrupti distinctio. Sit quia *dolor.*"
  5. Faker::TvShows::RuPaul.queen #=> "Violet Chachki"
  6. Faker::Alphanumeric.alpha(number: 10) #=> "zlvubkrwga"
  7. Faker::ProgrammingLanguage.name #=> "Ruby"

For a complete list of the generators, see Generators.

A note about the Generators versions

If you get a uninitialized constant Faker::[some_class] error, your version of
the gem is behind main.

To make sure that your gem is the one
documented here, change the line in your Gemfile to:

  1. gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'main'

The generators have the @faker.version tag on top of their implementation.
From the tags, you can identify the version the generator was added:

  1. # Faker::TvShows::ParksAndRec.character
  2. # @faker.version 1.9.0
  3. def character
  4. fetch('parks_and_rec.characters')
  5. end

Ensuring unique values

To ensure Faker generates unique values, prefix your method call with unique:

  1. Faker::Name.unique.name # This will return a unique name every time it is called

If too many unique values are requested from a generator that has a limited
number of potential values, a Faker::UniqueGenerator::RetryLimitExceeded
exception may be raised. It is possible to clear the record of unique values
that have been returned, for example between tests.

  1. Faker::Name.unique.clear # Clears used values for Faker::Name
  2. Faker::UniqueGenerator.clear # Clears used values for all generators

You also can give some already used values to the unique generator if you have
collisions with the generated data (i.e: using FactoryBot with random and
manually set values).

  1. # Usage:
  2. # Faker::<generator>.unique.exclude(method, arguments, list)
  3. # Add 'azerty' and 'wxcvbn' to the string generator with 6 char length
  4. Faker::Lorem.unique.exclude :string, [number: 6], %w[azerty wxcvbn]

Deterministic Random

Faker supports seeding of its pseudo-random number generator (PRNG)
to provide deterministic output of repeated method calls.

  1. Faker::Config.random = Random.new(42)
  2. Faker::Company.bs #=> "seize collaborative mindshare"
  3. Faker::Company.bs #=> "engage strategic platforms"
  4. Faker::Config.random = Random.new(42)
  5. Faker::Company.bs #=> "seize collaborative mindshare"
  6. Faker::Company.bs #=> "engage strategic platforms"
  7. Faker::Config.random = nil # seeds the PRNG using default entropy sources
  8. Faker::Config.random.seed #=> 185180369676275068918401850258677722187
  9. Faker::Company.bs #=> "cultivate viral synergies"

Customization

You may want Faker to print information depending on your location in the world.
To assist you in this, Faker uses the I18n gem to store strings and formats to
represent the names and postal codes of the area of your choosing.

Just set the locale you want as shown below, and Faker will take care of the rest.

  1. Faker::Config.locale = 'es'
  2. # or
  3. Faker::Config.locale = :es

To override Faker’s locales, and set it on threaded server environments
check out the locales README.

Minitest and Faker >= 2.22

To prevent Faker (version >= 2.22) from generating duplicate values when using Minitest,
you might need to add the following to the test_helper.rb or rails_helper.rb file:

  1. Faker::Config.random = Random.new

See Issue #2534 for more details.

Generators

This is the full list of generators available with this gem. If you need details about any of them, make sure to consult the documentation.

NOTE: Some of the generators below aren’t released yet. If you want to use them, change the line in your gemfile to:

  1. gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'main'

Default

- Faker::Address
- Faker::Alphanumeric
- Faker::Ancient
- Faker::App
- Faker::Appliance
- Faker::Artist
- Faker::Avatar
- Faker::Bank
- Faker::Barcode
- Faker::Beer
- Faker::Blood
- Faker::Boolean
- Faker::BossaNova
- Faker::Business
- Faker::Camera
- Faker::Cannabis
- Faker::ChileRut
- Faker::ChuckNorris
- Faker::Code
- Faker::Coffee
- Faker::Coin
- Faker::Color
- Faker::Commerce
- Faker::Company
- Faker::Compass
- Faker::Computer
- Faker::Construction
- Faker::Cosmere
- Faker::Crypto
- Faker::CryptoCoin
- Faker::Currency
- Faker::Date
- Faker::DcComics
- Faker::Demographic
- Faker::Dessert
- Faker::Device
- Faker::DrivingLicence
- Faker::Drone
- Faker::Educator
- Faker::ElectricalComponents
- Faker::Emotion
- Faker::Esport
- Faker::File
- Faker::Finance
- Faker::Food
- Faker::FunnyName
- Faker::Gender
- Faker::GreekPhilosophers
- Faker::Hacker
- Faker::Hipster
- Faker::Hobby
- Faker::House
- Faker::IdNumber
- Faker::IndustrySegments
- Faker::Internet
- Faker::Invoice
- Faker::Job
- Faker::Json
- Faker::Kpop
- Faker::Lorem
- Faker::LoremFlickr
- Faker::Markdown
- Faker::Marketing
- Faker::Measurement
- Faker::Military
- Faker::Mountain
- Faker::Name
- Faker::Nation
- Faker::NatoPhoneticAlphabet
- Faker::NationalHealthService
- Faker::Number
- Faker::Omniauth
- Faker::PhoneNumber
- Faker::Placeholdit
- Faker::ProgrammingLanguage
- Faker::Relationship
- Faker::Restaurant
- Faker::Science
- Faker::SlackEmoji
- Faker::Source
- Faker::SouthAfrica
- Faker::Space
- Faker::String
- Faker::Stripe
- Faker::Subscription
- Faker::Superhero
- Faker::Tea
- Faker::Team
- Faker::Theater
- Faker::Time
- Faker::Twitter
- Faker::Types
- Faker::University
- Faker::Vehicle
- Faker::Verbs
- Faker::VulnerabilityIdentifier
- Faker::WorldCup

Blockchain

- Faker::Blockchain::Aeternity
- Faker::Blockchain::Bitcoin
- Faker::Blockchain::Ethereum
- Faker::Blockchain::Tezos

Books

- Faker::Book
- Faker::Books::CultureSeries
- Faker::Books::Dune
- Faker::Books::Lovecraft
- Faker::Books::TheKingkillerChronicle

Fantasy

- Faker::Fantasy::Tolkien

Travel

- Faker:Travel::Airport
- Faker:Travel::TrainStation

Creature

- Faker::Creature::Animal
- Faker::Creature::Bird
- Faker::Creature::Cat
- Faker::Creature::Dog
- Faker::Creature::Horse

Games

- Faker::Game
- Faker::Games::ClashOfClans
- Faker::Games::DnD
- Faker::Games::Dota
- Faker::Games::ElderScrolls
- Faker::Games::Fallout
- Faker::Games::FinalFantasyXIV
- Faker::Games::HalfLife
- Faker::Games::Heroes
- Faker::Games::HeroesOfTheStorm
- Faker::Games::LeagueOfLegends
- Faker::Games::Minecraft
- Faker::Games::Myst
- Faker::Games::Overwatch
- Faker::Games::Pokemon
- Faker::Games::SonicTheHedgehog
- Faker::Games::StreetFighter
- Faker::Games::SuperMario
- Faker::Games::SuperSmashBros
- Faker::Games::Touhou
- Faker::Games::WarhammerFantasy
- Faker::Games::Witcher
- Faker::Games::WorldOfWarcraft
- Faker::Games::Zelda

Japanese Media

- Faker::JapaneseMedia::CowboyBebop
- Faker::JapaneseMedia::DragonBall
- Faker::JapaneseMedia::OnePiece
- Faker::JapaneseMedia::StudioGhibli
- Faker::JapaneseMedia::SwordArtOnline
- Faker::JapaneseMedia::Naruto
- Faker::JapaneseMedia::Doraemon
- Faker::JapaneseMedia::Conan
- Faker::JapaneseMedia::FullmetalAlchemistBrotherhood

Movies

- Faker::Movie
- Faker::Movies::Avatar
- Faker::Movies::BackToTheFuture
- Faker::Movies::Departed
- Faker::Movies::Ghostbusters
- Faker::Movies::HarryPotter
- Faker::Movies::HitchhikersGuideToTheGalaxy
- Faker::Movies::Hobbit
- Faker::Movies::HowToTrainYourDragon
- Faker::Movies::Lebowski
- Faker::Movies::LordOfTheRings
- Faker::Movies::PrincessBride
- Faker::Movies::StarWars
- Faker::Movies::TRON
- Faker::Movies::VForVendetta

Music

- Faker::Music
- Faker::Music::GratefulDead
- Faker::Music::Hiphop
- Faker::Music::Opera
- Faker::Music::PearlJam
- Faker::Music::Phish
- Faker::Music::Prince
- Faker::Music::RockBand
- Faker::Music::Rush
- Faker::Music::SmashingPumpkins
- Faker::Music::UmphreysMcgee

Quotes

- Faker::Quote
- Faker::Quotes::Chiquito
- Faker::Quotes::Rajnikanth
- Faker::Quotes::Shakespeare

Sports

- Faker::Sport
- Faker::Sports::Basketball
- Faker::Sports::Chess
- Faker::Sports::Football
- Faker::Sports::Mountaineering
- Faker::Sports::Volleyball

Tv Shows

- Faker::TvShows::AquaTeenHungerForce
- Faker::TvShows::Archer
- Faker::TvShows::BigBangTheory
- Faker::TvShows::BojackHorseman
- Faker::TvShows::BreakingBad
- Faker::TvShows::BrooklynNineNine
- Faker::TvShows::Buffy
- Faker::TvShows::Community
- Faker::TvShows::DrWho
- Faker::TvShows::DumbAndDumber
- Faker::TvShows::FamilyGuy
- Faker::TvShows::FinalSpace
- Faker::TvShows::Friends
- Faker::TvShows::GameOfThrones
- Faker::TvShows::HeyArnold
- Faker::TvShows::HowIMetYourMother
- Faker::TvShows::MichaelScott
- Faker::TvShows::NewGirl
- Faker::TvShows::ParksAndRec
- Faker::TvShows::RickAndMorty
- Faker::TvShows::RuPaul
- Faker::TvShows::Seinfeld
- Faker::TvShows::SiliconValley
- Faker::TvShows::Simpsons
- Faker::TvShows::SouthPark
- Faker::TvShows::Spongebob
- Faker::TvShows::StarTrek
- Faker::TvShows::Stargate
- Faker::TvShows::StrangerThings
- Faker::TvShows::Suits
- Faker::TvShows::Supernatural
- Faker::TvShows::TheExpanse
- Faker::TvShows::TheFreshPrinceOfBelAir
- Faker::TvShows::TheITCrowd
- Faker::TvShows::TheThickOfIt
- Faker::TvShows::TwinPeaks
- Faker::TvShows::VentureBros

Locations

- Faker::Locations::Australia

Contributing

Note: We are not accepting proposals for new generators and locales. The Contributing guide has a few notes about this decision.

Take a look at the Contributing document for
instructions on setting up the repo on your machine, opening bug reports, understanding the codebase,
and creating a good pull request.

There is a Discord channel to discuss anything
regarding improvements or feature requests.

Thank you, contributors!

Versioning

Faker follows Semantic Versioning 2.0 as defined at https://semver.org.

License

This code is free to use under the terms of the MIT license.