= RSpec/FactoryBot

== RSpec/FactoryBot/AttributeDefinedStatically

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Yes
| 1.28
| 2.0
|===

Always declare attribute values as blocks.

=== Examples

[source,ruby]
----
# bad
kind [:active, :rejected].sample

# good
kind { [:active, :rejected].sample }

# bad
closed_at 1.day.from_now

# good
closed_at { 1.day.from_now }

# bad
count 1

# good
count { 1 }
----

=== Configurable attributes

|===
| Name | Default value | Configurable values

| Include
| `spec/factories.rb`, `+spec/factories/**/*.rb+`, `+features/support/factories/**/*.rb+`
| Array
|===

=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/AttributeDefinedStatically

== RSpec/FactoryBot/ConsistentParenthesesStyle

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Yes
| 2.14
| -
|===

Use a consistent style for parentheses in factory bot calls.

=== Examples

[source,ruby]
----
# bad
create :user
build(:user)
create(:login)
create :login
----

==== `EnforcedStyle: require_parentheses` (default)

[source,ruby]
----
# good
create(:user)
create(:user)
create(:login)
build(:login)
----

==== `EnforcedStyle: omit_parentheses`

[source,ruby]
----
# good
create :user
build :user
create :login
create :login

# also good
# when method name and first argument are not on same line
create(
  :user
)
build(
  :user,
  name: 'foo'
)
----

=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `require_parentheses`
| `require_parentheses`, `omit_parentheses`
|===

=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/ConsistentParenthesesStyle

== RSpec/FactoryBot/CreateList

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Yes
| 1.25
| 2.0
|===

Checks for create_list usage.

This cop can be configured using the `EnforcedStyle` option

=== Examples

==== `EnforcedStyle: create_list` (default)

[source,ruby]
----
# bad
3.times { create :user }

# good
create_list :user, 3

# bad
3.times { create :user, age: 18 }

# good - index is used to alter the created models attributes
3.times { |n| create :user, age: n }

# good - contains a method call, may return different values
3.times { create :user, age: rand }
----

==== `EnforcedStyle: n_times`

[source,ruby]
----
# bad
create_list :user, 3

# good
3.times { create :user }
----

=== Configurable attributes

|===
| Name | Default value | Configurable values

| Include
| `+**/*_spec.rb+`, `+**/spec/**/*+`, `spec/factories.rb`, `+spec/factories/**/*.rb+`, `+features/support/factories/**/*.rb+`
| Array

| EnforcedStyle
| `create_list`
| `create_list`, `n_times`
|===

=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/CreateList

== RSpec/FactoryBot/FactoryClassName

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Yes
| 1.37
| 2.0
|===

Use string value when setting the class attribute explicitly.

This cop would promote faster tests by lazy-loading of
application files. Also, this could help you suppress potential bugs
in combination with external libraries by avoiding a preload of
application files from the factory files.

=== Examples

[source,ruby]
----
# bad
factory :foo, class: Foo do
end

# good
factory :foo, class: 'Foo' do
end
----

=== Configurable attributes

|===
| Name | Default value | Configurable values

| Include
| `spec/factories.rb`, `+spec/factories/**/*.rb+`, `+features/support/factories/**/*.rb+`
| Array
|===

=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/FactoryClassName

== RSpec/FactoryBot/FactoryNameStyle

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Yes
| 2.16
| -
|===

Checks for name style for argument of FactoryBot::Syntax::Methods.

=== Examples

==== EnforcedStyle: symbol (default)

[source,ruby]
----
# bad
create('user')
build "user", username: "NAME"

# good
create(:user)
build :user, username: "NAME"
----

==== EnforcedStyle: string

[source,ruby]
----
# bad
create(:user)
build :user, username: "NAME"

# good
create('user')
build "user", username: "NAME"
----

=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `symbol`
| `symbol`, `string`
|===

=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/FactoryNameStyle

== RSpec/FactoryBot/SyntaxMethods

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Yes (Unsafe)
| 2.7
| -
|===

Use shorthands from `FactoryBot::Syntax::Methods` in your specs.

=== Safety

The autocorrection is marked as unsafe because the cop
cannot verify whether you already include
`FactoryBot::Syntax::Methods` in your test suite.

If you're using Rails, add the following configuration to
`spec/support/factory_bot.rb` and be sure to require that file in
`rails_helper.rb`:

[source,ruby]
----
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end
----

If you're not using Rails:

[source,ruby]
----
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods

  config.before(:suite) do
    FactoryBot.find_definitions
  end
end
----

=== Examples

[source,ruby]
----
# bad
FactoryBot.create(:bar)
FactoryBot.build(:bar)
FactoryBot.attributes_for(:bar)

# good
create(:bar)
build(:bar)
attributes_for(:bar)
----

=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/SyntaxMethods
