0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

factorybotの説明を読んだ

Posted at

設定

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_railscustom/factories.rbcustom/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.rbtest/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]

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?