FactoryBot

FactoryBot/AssociationStyle

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

No

Always (Unsafe)

2.23

2.24

Use a consistent style to define associations.

Safety

This cop may cause false-positives in EnforcedStyle: explicit case. It recognizes any method call that has no arguments as an implicit association but it might be a user-defined trait call.

Examples

EnforcedStyle: implicit (default)

# bad
factory :post do
  association :user
end

# good
factory :post do
  user
end

# bad
factory :post do
  association :user, :author
end

# good
factory :post do
  user factory: %i[user author]
end

EnforcedStyle: explicit

# bad
factory :post do
  user
end

# good
factory :post do
  association :user
end

# bad
factory :post do
  user factory: %i[user author]
end

# good
factory :post do
  association :user, :author
end

# good (NonImplicitAssociationMethodNames: ['email'])
sequence :email do |n|
  "person#{n}@example.com"
end

factory :user do
  email
end

Configurable attributes

Name Default value Configurable values

EnforcedStyle

implicit

explicit, implicit

NonImplicitAssociationMethodNames

<none>

FactoryBot/AttributeDefinedStatically

Enabled by default Safe Supports autocorrection Version Added Version Changed

Enabled

Yes

Always

1.28

2.24

Always declare attribute values as blocks.

Examples

# 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 }

FactoryBot/ConsistentParenthesesStyle

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

Always

2.14

2.23

Use a consistent style for parentheses in factory_bot calls.

Examples

EnforcedStyle: require_parentheses (default)

# bad
create :user
build :login

# good
create(:user)
build(:login)

EnforcedStyle: omit_parentheses

# bad
create(:user)
build(:login)

# good
create :user
build :login

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

ExplicitOnly: false (default)

# bad - with `EnforcedStyle: require_parentheses`
FactoryBot.create :user
build :user

# good - with `EnforcedStyle: require_parentheses`
FactoryBot.create(:user)
build(:user)

ExplicitOnly: true

# bad - with `EnforcedStyle: require_parentheses`
FactoryBot.create :user
FactoryBot.build :user

# good - with `EnforcedStyle: require_parentheses`
FactoryBot.create(:user)
FactoryBot.build(:user)
create :user
build :user

Configurable attributes

Name Default value Configurable values

Include

**/*_spec.rb, **/spec/**/*, **/test/**/*, **/features/support/factories/**/*.rb

Array

EnforcedStyle

require_parentheses

require_parentheses, omit_parentheses

ExplicitOnly

false

Boolean

FactoryBot/CreateList

Enabled by default Safe Supports autocorrection Version Added Version Changed

Enabled

Yes

Command-line only (Unsafe)

1.25

2.26

Checks for create_list usage.

This cop can be configured using the EnforcedStyle option

Safety

This cop’s autocorrection is unsafe because replacing n.times to create_list changes its returned value.

Examples

EnforcedStyle: create_list (default)

# bad
3.times { create :user }
3.times.map { create :user }
[create(:user), create(:user), create(:user)]
Array.new(3) { 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

# bad
create_list :user, 3
[create(:user), create(:user), create(:user)]

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

ExplicitOnly: false (default)

# bad - with `EnforcedStyle: create_list`
3.times { FactoryBot.create :user }
3.times { create :user }

# good - with `EnforcedStyle: create_list`
FactoryBot.create_list :user, 3
create_list :user, 3

ExplicitOnly: true

# bad - with `EnforcedStyle: create_list`
3.times { FactoryBot.create :user }

# good - with `EnforcedStyle: create_list`
FactoryBot.create_list :user, 3
create_list :user, 3
3.times { create :user }

Configurable attributes

Name Default value Configurable values

Include

**/*_spec.rb, **/spec/**/*, **/test/**/*, **/features/support/factories/**/*.rb

Array

EnforcedStyle

create_list

create_list, n_times

ExplicitOnly

false

Boolean

FactoryBot/ExcessiveCreateList

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

No

2.25

-

Check for excessive model creation in a list.

Examples

MaxAmount: 10 (default)

# We do not allow more than 10 items to be created

# bad
create_list(:merge_request, 1000, state: :opened)

# good
create_list(:merge_request, 10, state: :opened)

MaxAmount: 20

# We do not allow more than 20 items to be created

# bad
create_list(:merge_request, 1000, state: :opened)

# good
create_list(:merge_request, 15, state: :opened)

Configurable attributes

Name Default value Configurable values

Include

**/*_spec.rb, **/spec/**/*, **/test/**/*, **/features/support/factories/**/*.rb

Array

MaxAmount

10

Integer

FactoryBot/FactoryAssociationWithStrategy

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

No

2.23

2.23

Use definition in factory association instead of hard coding a strategy.

Examples

# bad - only works for one strategy
factory :foo do
  profile { create(:profile) }
end

# good - implicit
factory :foo do
  profile
end

# good - explicit
factory :foo do
  association :profile
end

# good - inline
factory :foo do
  profile { association :profile }
end

Configurable attributes

Name Default value Configurable values

Include

**/*_spec.rb, **/spec/**/*, **/test/**/*, **/features/support/factories/**/*.rb

Array

FactoryBot/FactoryClassName

Enabled by default Safe Supports autocorrection Version Added Version Changed

Enabled

Yes

Always

1.37

2.24

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

# bad
factory :foo, class: Foo do
end

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

FactoryBot/FactoryNameStyle

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

Always

2.16

2.23

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

Examples

EnforcedStyle: symbol (default)

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

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

# good - namespaced models
create('users/internal')

EnforcedStyle: string

# bad
create(:user)
build :user, username: "NAME"

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

ExplicitOnly: false (default)

# bad - with `EnforcedStyle: symbol`
FactoryBot.create('user')
create('user')

# good - with `EnforcedStyle: symbol`
FactoryBot.create(:user)
create(:user)

ExplicitOnly: true

# bad - with `EnforcedStyle: symbol`
FactoryBot.create(:user)
FactoryBot.build "user", username: "NAME"

# good - with `EnforcedStyle: symbol`
FactoryBot.create('user')
FactoryBot.build "user", username: "NAME"
FactoryBot.create(:user)
create(:user)

Configurable attributes

Name Default value Configurable values

Include

**/*_spec.rb, **/spec/**/*, **/test/**/*, **/features/support/factories/**/*.rb

Array

EnforcedStyle

symbol

symbol, string

ExplicitOnly

false

Boolean

FactoryBot/IdSequence

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

Always

2.24

-

Do not create a FactoryBot sequence for an id column.

Examples

# bad - can lead to conflicts between FactoryBot and DB sequences
factory :foo do
  sequence :id
end

# good - a non-id column
factory :foo do
  sequence :some_non_id_column
end

FactoryBot/RedundantFactoryOption

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

Always

2.23

-

Checks for redundant factory option.

Examples

# bad
association :user, factory: :user

# good
association :user

Configurable attributes

Name Default value Configurable values

Include

**/*_spec.rb, **/spec/**/*, **/test/**/*, **/features/support/factories/**/*.rb

Array

FactoryBot/SyntaxMethods

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

Always (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:

RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end

If you’re not using Rails:

RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods

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

Examples

# bad
FactoryBot.create(:bar)
FactoryBot.build(:bar)
FactoryBot.attributes_for(:bar)

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

Configurable attributes

Name Default value Configurable values

Include

**/*_spec.rb, **/spec/**/*, **/test/**/*, **/features/support/factories/**/*.rb

Array