mirror of
https://github.com/thoughtbot/factory_bot_rails.git
synced 2022-11-09 11:49:18 -05:00
Update documentation to include new configuration
Added documentation for the definition_file_paths configuration. Also restructured and expanded a bit and added an example of the `filename_proc` option.
This commit is contained in:
parent
4e730548cb
commit
08fd922fbf
1 changed files with 80 additions and 19 deletions
99
README.md
99
README.md
|
@ -12,9 +12,7 @@ Check out the [guide](https://github.com/thoughtbot/factory_bot/blob/4-9-0-stabl
|
||||||
|
|
||||||
## Rails
|
## Rails
|
||||||
|
|
||||||
factory_bot_rails provides Rails integration for [factory_bot][fb].
|
factory\_bot\_rails provides Rails integration for [factory_bot][fb].
|
||||||
|
|
||||||
Currently, automatic factory definition loading is the only Rails-specific feature.
|
|
||||||
|
|
||||||
Supported Rails versions are listed in [`Appraisals`](Appraisals). Supported
|
Supported Rails versions are listed in [`Appraisals`](Appraisals). Supported
|
||||||
Ruby versions are listed in [`.travis.yml`](.travis.yml).
|
Ruby versions are listed in [`.travis.yml`](.travis.yml).
|
||||||
|
@ -37,9 +35,56 @@ group :development, :test do
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
Generators for factories will automatically substitute fixture (and maybe any other
|
You may want to configure your test suite to include factory\_bot methods; see
|
||||||
`fixture_replacement` you set). If you want to disable this feature, add the
|
[configuration](https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#configure-your-test-suite).
|
||||||
following to your application.rb file:
|
|
||||||
|
### Automatic Factory Definition Loading
|
||||||
|
|
||||||
|
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
|
||||||
|
```
|
||||||
|
|
||||||
|
You can configure by adding the following to "config/application.rb" or the
|
||||||
|
appropriate environment configuration in "config/environments":
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
config.factory_bot.definition_file_paths = ["custom/factories"]
|
||||||
|
```
|
||||||
|
|
||||||
|
This will cause factory\_bot\_rails to automatically load factories in
|
||||||
|
`custom/factories.rb` and `custom/factories/*.rb`.
|
||||||
|
|
||||||
|
It is possible to use this setting to share factories from a gem:
|
||||||
|
|
||||||
|
```rb
|
||||||
|
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:
|
||||||
|
|
||||||
|
```rb
|
||||||
|
config.factory_bot.definition_file_paths = []
|
||||||
|
```
|
||||||
|
|
||||||
|
### Generators
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
config.generators do |g|
|
config.generators do |g|
|
||||||
|
@ -47,8 +92,15 @@ config.generators do |g|
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
Default factories directory is `test/factories`, or `spec/factories` if
|
If fixture replacement is enabled and you already have a `test/factories.rb`
|
||||||
`test_framework` generator is set to `:rspec`; change this behavior with:
|
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:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
config.generators do |g|
|
config.generators do |g|
|
||||||
|
@ -56,20 +108,29 @@ config.generators do |g|
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
If you use factory_bot for fixture replacement, ensure that
|
Note that factory\_bot\_rails will not automatically load files in custom
|
||||||
factory_bot_rails is available in the development group. If it's not, Rails
|
locations unless you add them to `config.factory_bot.defintion_file_paths` as
|
||||||
will generate standard .yml files instead of factory files.
|
well.
|
||||||
|
|
||||||
factory_bot takes an option `suffix: 'some_suffix'` to generate factories as
|
The suffix option allows you to customize the name of the generated file with a
|
||||||
`modelname_some_suffix.rb`.
|
suffix:
|
||||||
|
|
||||||
If you use factory_bot for fixture replacement and already have a
|
```ruby
|
||||||
`factories.rb` file in the directory that contains your tests,
|
config.generators do |g|
|
||||||
factory_bot_rails will insert new factory definitions at the top of
|
g.factory_bot suffix: "factory"
|
||||||
`factories.rb`.
|
end
|
||||||
|
```
|
||||||
|
|
||||||
You may need to configure your test suite to include factory_bot methods; see
|
This will generate `test/factories/users_factory.rb` instead of
|
||||||
[configuration](https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#configure-your-test-suite).
|
`test/factories/users.rb`.
|
||||||
|
|
||||||
|
For even more customization, use the `filename_proc` option:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
config.generators do |g|
|
||||||
|
g.factory_bot filename_proc: -> { |table_name| "prefix_#{table_name}_suffix" }
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue