Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major docs overhaul: refs, guide, cookbook
Split the docs into reference, guide, and cookbook. The guide is mostly as it was before, with a lot of cleanup. Some sections were combined and simplified. Some guides were moved into a new cookbook section. This section describes techniques that are helpful in specific situations of using factory_bot but not, in itself, part of factory_bot. For example, using factory_bot to simulate a has_many relation. Other guides were moved into the wiki. The criteria for this was: does it have to do with factory_bot, or with something external? Will we change the docs alongside a change in this repo's code, or due to another repo's code change? A new reference section is added. It lists terse facts about methods you are expected to use. The reason RubyDoc isn't the right solution here is because these methods are defined all over the codebase. Due to `instance_eval` and mixins, factory_bot is more of a DSL than a set of Ruby classes. Thus, the reference section. During the review, we updated the name of the second parameter for hooks: instead of `evaluator`, we call it the `context`. Co-authored-by: Sara Jackson <csara@thoughtbot.com> Co-authored-by: Stefanni Brasil <stefannibrasil@gmail.com>
- Loading branch information
1 parent
f870bbe
commit fbea96b
Showing
49 changed files
with
805 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,19 @@ | ||
# Callbacks | ||
|
||
factory\_bot makes four callbacks available: | ||
|
||
* after(:build) - called after a factory is built (via `FactoryBot.build`, `FactoryBot.create`) | ||
* before(:create) - called before a factory is saved (via `FactoryBot.create`) | ||
* after(:create) - called after a factory is saved (via `FactoryBot.create`) | ||
* after(:stub) - called after a factory is stubbed (via `FactoryBot.build_stubbed`) | ||
|
||
Examples: | ||
|
||
```ruby | ||
# Define a factory that calls the generate_hashed_password method after the user factory is built | ||
factory :user do | ||
after(:build) { |user, context| generate_hashed_password(user) } | ||
end | ||
``` | ||
|
||
Note that you'll have an instance of the object in the block. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# add_attribute | ||
|
||
Within a factory definition, the `add_attribute` method defines a key/value | ||
pair that will be set when the object is built. | ||
|
||
The `add_attribute` method takes two arguments: a name (Symbol or String) and a | ||
block. This block is called each time this object is constructed. The block is | ||
not called when the attribute is overriden by a build strategy. | ||
|
||
Assignment is done by calling the Ruby attribute setter. For example, given | ||
|
||
```ruby | ||
FactoryBot.define do | ||
factory :user do | ||
add_attribute(:name) { "Acid Burn" } | ||
end | ||
end | ||
``` | ||
|
||
This will use the `#name=` setter: | ||
|
||
```ruby | ||
user = User.new | ||
user.name = "Acid Burn" | ||
``` | ||
|
||
Also see [method_missing](method_missing.html) for a shorthand. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# association | ||
|
||
Within a factory block, use the `association` method to always make an | ||
additional object alongside this one. This name best makes sense within the | ||
context of ActiveRecord. | ||
|
||
The `association` method takes a mandatory name and optional options. | ||
|
||
The options are zero or more trait names (Symbols), followed by a hash | ||
of attribute overrides. When constructing this association, factory\_bot uses | ||
the trait and attribute overrides given. | ||
|
||
See [method_missing](method_missing.html) for a shorthand. See [build | ||
strategies](build-strategies.html) for an explanation of how each build | ||
strategy handles associations. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# skip_create, to_create, and initialize_with | ||
|
||
The `skip_create`, `to_create`, and `initialize_with` methods control how | ||
factory\_bot interacts with the [build strategies](build-strategies.html). | ||
|
||
These methods can be called within a `factory` definition block, to scope their | ||
effects to just that factory; or within `FactoryBot.define`, to affect global | ||
change. | ||
|
||
## initialize_with | ||
|
||
The `initialize_with` method takes a block and returns an instance of the | ||
factory's class. It has access to the `attributes` method, which is a hash of | ||
all the fields and values for the object. | ||
|
||
The default definition is: | ||
|
||
```ruby | ||
initialize_with { new } | ||
``` | ||
|
||
## to_create | ||
|
||
The `to_create` method lets you control the `FactoryBot.create` strategy. This | ||
method takes a block which takes the object as constructed by | ||
`initialize_with`, and the factory\_bot context. The context has additional | ||
data from any [`transient`] blocks. | ||
|
||
[`transient`]: transient.html | ||
|
||
The default definition is: | ||
|
||
```ruby | ||
to_create { |obj, context| obj.save! } | ||
``` | ||
|
||
The `skip_create` method is a shorthand for turning `to_create` into a no-op. | ||
This allows you to use the `create` strategy as a synonym for `build`, except | ||
you additionally get any `create` hooks. |
Oops, something went wrong.