設定
Setup
いくつかあれば、あなたが使うフレームでインストール方法そしてオプションでテストフレームワークが異なります
Installation varies based on the framework you are using, if any, and optionally the test framework.
コントロールしないコードに基づいてインストール方法の変更するので、ドキュメントはwikiで最新を保ち続けられる。フレームワークを変化に合わせてwikiを編集することを推奨します。
Since installation varies based on code that we do not control, those docs are kept up-to-date in our wiki. We encourage you to edit the wiki as the frameworks change.
添付したごくありふれたセットアップに従って下さい。しかしwikiでより詳細を説明しています
Below we document the most common setup. However, we go into more detail in our wiki.
自動的なファクトリー定義の読み込み
Automatic Factory Definition Loading
デフォルトによって、factory_bot_rails
はRailsのプロジェクトのルートに対して以下の位置で定義されたファクトリーを自動的に読み込みます。
By default, factory_bot_rails will automatically load factories defined in the following locations, relative to the root of the Rails project:
- factories.rb
- test/factories.rb
- spec/factories.rb
- factories/*.rb
- test/factories/*.rb
- spec/factories/*.rb
config/application.rb
またはconfig/environments
内の適切な環境設定に以下を追加によって設定できる
You can configure by adding the following to config/application.rb or the appropriate environment configuration in config/environments:
config.factory_bot.definition_file_paths = ["custom/factories"]
これはfactory_bot_rails
はcustom/factories.rb
、custom/factories/*.rb.内のファクトリー
を自動的に読み込むことを引き起こす
This will cause factory_bot_rails to automatically load factories in custom/factories.rb and custom/factories/*.rb.
この設定を使ってgemからファクトリーを共有することは可能です
It is possible to use this setting to share factories from a gem:
begin
require 'factory_bot_rails'
rescue LoadError
end
class MyEngine < ::Rails::Engine
config.factory_bot.definition_file_paths +=
[File.expand_path('../factories', __FILE__)] if defined?(FactoryBotRails)
end
空の配列を使うことによって自動的なファクトリー定義読み込みを完全に使用不可にすることもできます
You can also disable automatic factory definition loading entirely by using an empty array:
config.factory_bot.definition_file_paths = []
ファイルの備え付けサポート
File Fixture Support
ファクトリーはテストからファイルを読み込むためにてActiveSupport::Testing::FileFixtures#file_fixture helper
にアクセスします。備え付けサポートを無効にするにはfile_fixture_support = false
を設定して下さい
Factories have access to ActiveSupport::Testing::FileFixtures#file_fixture helper to read files from tests.
To disable file fixture support, set file_fixture_support = false:
config.factory_bot.file_fixture_support = false
生成機
Generators
gemファイルのdevelopment
グループ内にfactory_bot_rails
を含めることはRailsがfixtureの代わりファクトリーを生成するようになる。この機能を無効にしたい場合、Gemfileのdevelopment
グループの外に factory_bot_rails
を移すまたは以下の設定を追加いずれかをできる。
Including factory_bot_rails in the development group of your Gemfile will cause Rails to generate factories instead of fixtures. If you want to disable this feature, you can either move factory_bot_rails out of the development group of your Gemfile, or add the following configuration:
config.generators do |g|
g.factory_bot false
end
fixture交換が可能かつ既にtest/factories.rb
ファイル(またはrspec_rails
を使っている場合はspec/factories.rb
)を持っている場合、生成されたファクトリーは存在するファイルの先頭に挿入されるだろう。別な方法で、ファクトリーはtest/factories
ディレクトリ(rspec_rails
を使っている場合はspec/factories.rb
)のテーブルの名前と一致するファイル内(つまりtest/factories/users.rb)で生成されるだろう
If fixture replacement is enabled and you already have a test/factories.rb file (or spec/factories.rb if using rspec_rails), generated factories will be inserted at the top of the existing file. Otherwise, factories will be generated in the test/factories directory (spec/factories if using rspec_rails), in a file matching the name of the table (e.g. test/factories/users.rb).
異なるディレクトリ内でファクトリーを生成するには、以下の設定を使える
To generate factories in a different directory, you can use the following configuration:
config.generators do |g|
g.factory_bot dir: 'custom/dir/for/factories'
end
なおconfig.factory_bot.definition_file_paths
にカスタムした位置を追加しない限りfactory_bot_rails
はカスタムの位置では自動的にファイルを読み込ないことに注意して下さい。
Note that factory_bot_rails will not automatically load files in custom locations unless you add them to config.factory_bot.definition_file_paths as well.
接尾辞オプションは接尾辞をつけた生成されたファイルの名前をカスタマイズすることを許可します。
The suffix option allows you to customize the name of the generated file with a suffix:
config.generators do |g|
g.factory_bot suffix: "factory"
end
これ(上)はtest/factories/users.rb
のtest/factories/users_factory.rb
が生成されました。
This will generate test/factories/users_factory.rb instead of test/factories/users.rb.
より多くのカスタムのためにfilename_proc
オプションを使って下さい
For even more customization, use the filename_proc option:
config.generators do |g|
g.factory_bot filename_proc: ->(table_name) { "prefix_#{table_name}_suffix" }
end
初期のファクトリーテンプレートをオーバーライドするには、lib/templates/factory_bot/model/factories.erb
の独自のテンプレートを定義して下さい。このテンプレートはFactoryBot::Generators::ModelGenerator
内で使用可能なメソッドにアクセスすることができる。factory_bot_rails
は別々のファイルで各ファクトリーを生成する場合、factory_bot_rails
がカスタムテンプレートのみの使用に注意しましょう。test/factories.rb
またはspec/factories.rb
内でファクトリーの全てを生成する場合は、スタムテンプレートを使用されない。
To override the default factory template, define your own template in lib/templates/factory_bot/model/factories.erb. This template will have access to any methods available in FactoryBot::Generators::ModelGenerator. Note that factory_bot_rails will only use this custom template if you are generating each factory in a separate file; it will have no effect if you are generating all of your factories in test/factories.rb or spec/factories.rb.
Factory_bot_rails
はカスタムジェネレーターを追加する
Factory_bot_rails will add a custom generator:
rails generate factory_bot:model NAME [field:type field:type] [options]