faker README

The project's README from github.com/faker-ruby/faker. As one markdown file: README.md.

Faker Gem Version Build Status Total Downloads Downloads

Generate (almost) realistic fake data for testing, demos, and populating your database during development.

Features

Note: The generated names, addresses, emails, phone numbers, and/or other data might return valid information. Please be careful when using faker in your tests.

For a complete list of the generators, see Generators.

Getting Started

Start by including faker in your Gemfile:

gem 'faker'

Then run bundle install.

Usage

require 'faker'

Faker::Name.name                      #=> "Christophe Bartell"
Faker::Internet.password              #=> "Vg5mSvY1UeRg7"
Faker::Internet.email                 #=> "[email protected]"
Faker::Address.full_address           #=> "5479 William Way, East Sonnyhaven, LA 63637"
Faker::Markdown.emphasis              #=> "Quo qui aperiam. Amet corrupti distinctio. Sit quia *dolor.*"
Faker::Lorem.paragraph                #=> "Recusandae minima consequatur. Expedita sequi blanditiis. Ut fuga et."
Faker::Alphanumeric.alpha(number: 10) #=> "zlvubkrwga"
Faker::ProgrammingLanguage.name       #=> "Ruby"

Table of Contents

Notes

Ensuring unique values

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

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.

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

You can also 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).

# Faker::<generator>.unique.exclude(method, arguments, list)

# Add 'azerty' and 'wxcvbn' to the string generator with 6 char length
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.

Faker::Config.random = Random.new(42)
Faker::Lorem.word              #=> "velit"
Faker::Lorem.word              #=> "quisquam"

Faker::Config.random = Random.new(42)
Faker::Lorem.word              #=> "velit"
Faker::Lorem.word              #=> "quisquam"

Faker::Config.random = nil     # seeds the PRNG using default entropy sources
Faker::Config.random.seed      #=> 185180369676275068918401850258677722187
Faker::Lorem.word              #=> "ipsam"

Localization

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.

Faker::Config.locale = 'es'
# or
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:

Faker::Config.random = Random.new

Generators

To see the full list, check out the GENERATORS document.

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. This is not actively monitored by the current maintainers.

Thank you, contributors!

Versioning

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

Inspiration

Faker was inspired by Perl's Data::Faker library.

In the media

License

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

faker (faker-ruby/faker)

Searching Database…

11.9K ·GitHub
这是什么?Faker 是一个 Ruby 库,可以生成看起来很真实的假数据(姓名、地址、电子邮件、电话号码等),用于测试、演示,以及为数据库填充种子数据。
它是独立的应用,还是我要添加到代码里的库?它是一个 Ruby gem,作为依赖项添加到你的项目中,而不是独立的应用程序。
怎样把它添加到项目里?将 faker 添加到 Gemfile 中,然后运行 'bundle install',接着在代码中调用它的生成方法即可。
除了 Ruby 和 Bundler,还需要别的东西吗?不需要额外的服务或 API 密钥;它内部只依赖 I18n gem 来处理语言区域设置。
生成的假数据总是唯一的,或者可以安全使用吗?默认情况下,返回的值不保证唯一,生成的数据也可能偶然与真实信息相同,因此需要时应在调用前加上 'unique',并且在测试中要谨慎使用。
我能让每次运行生成的假数据都可以复现吗?可以,Faker 支持为其伪随机数生成器(PRNG)设置种子,使重复调用产生确定的输出。
Faker 能生成英语以外的语言的数据吗?可以,它支持 40 多种语言区域,只要设置 I18n 语言区域,就能生成不同地区的真实姓名、地址和电话号码。
Faker 能生成哪些类别的假数据?它涵盖互联网数据(电子邮件、密码、域名)、日期和时间、人员信息(姓名、性别、职位)、数字和字符串、位置信息(地址、邮政编码),以及金融数据(账户详情、交易记录、加密货币地址)。
新的数据生成器和语言区域还在不断添加吗?不会了,维护者已表示不再接受新数据生成器和语言区域的提案。
What are similar projects?faker, faker, json-server, vibe-aso, Faker, msw, mockito, nock
Creator
faker-ruby
Size
14.7 MB
Stars
11.9K
Age
17y
Commits · 90 Days