Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 106

RSpec/ExampleLength:
Exclude:
- spec/rubocop/cop/rspec/factory_bot/attribute_defined_statically_spec.rb
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ~ [45/30].

Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,47 @@
factory :post do
title "Something"
^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
comments_count 0
^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
tag Tag::MAGIC
^^^^^^^^^^^^^^ Use a block to declare attribute values.
recent_statuses []
^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
status([:draft, :published].sample)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
published_at 1.day.from_now
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
status [:draft, :published].sample
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
created_at 1.day.ago
^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
created_at(1.day.ago)
^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
update_times [Time.current]
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
meta_tags(foo: Time.current)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
other_tags({ foo: Time.current })
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
options color: :blue
^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
other_options Tag::MAGIC => :magic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
end
end
RUBY

expect_correction(<<-RUBY)
FactoryBot.define do
factory :post do
title { "Something" }
comments_count { 0 }
tag { Tag::MAGIC }
recent_statuses { [] }
status { [:draft, :published].sample }
published_at { 1.day.from_now }
created_at { 1.day.ago }
update_times { [Time.current] }
meta_tags { { foo: Time.current } }
other_tags { { foo: Time.current } }
options { { color: :blue } }
other_options { { Tag::MAGIC => :magic } }
end
end
RUBY
Expand All @@ -35,6 +66,17 @@
end
end
RUBY

expect_correction(<<-RUBY)
FactoryBot.define do
factory :post do
trait :published do
title { "Something" }
published_at { 1.day.from_now }
end
end
end
RUBY
end

it 'registers an offense in a transient block' do
Expand All @@ -50,6 +92,17 @@
end
end
RUBY

expect_correction(<<-RUBY)
FactoryBot.define do
factory :post do
transient do
title { "Something" }
published_at { 1.day.from_now }
end
end
end
RUBY
end

it 'registers an offense for an attribute defined on `self`' do
Expand All @@ -62,6 +115,15 @@
end
end
RUBY

expect_correction(<<-RUBY)
FactoryBot.define do
factory :post do
self.start { Date.today }
self.end { Date.tomorrow }
end
end
RUBY
end

it 'registers an offense for attributes defined on explicit receiver' do
Expand All @@ -77,6 +139,17 @@
end
end
RUBY

expect_correction(<<-RUBY)
FactoryBot.define do
factory :post do |post_definition|
post_definition.end { Date.tomorrow }
post_definition.trait :published do |published_definition|
published_definition.published_at { 1.day.from_now }
end
end
end
RUBY
end

it 'accepts valid factory definitions' do
Expand Down Expand Up @@ -161,56 +234,4 @@
end
RUBY
end

bad = <<-RUBY
FactoryBot.define do
factory :post do
title "Something"
comments_count 0
tag Tag::MAGIC
recent_statuses []
status([:draft, :published].sample)
published_at 1.day.from_now
created_at(1.day.ago)
updated_at Time.current
update_times [Time.current]
meta_tags(foo: Time.current)
other_tags({ foo: Time.current })
options color: :blue
other_options Tag::MAGIC => :magic
self.end Date.tomorrow

trait :old do
published_at 1.week.ago
end
end
end
RUBY

corrected = <<-RUBY
FactoryBot.define do
factory :post do
title { "Something" }
comments_count { 0 }
tag { Tag::MAGIC }
recent_statuses { [] }
status { [:draft, :published].sample }
published_at { 1.day.from_now }
created_at { 1.day.ago }
updated_at { Time.current }
update_times { [Time.current] }
meta_tags { { foo: Time.current } }
other_tags { { foo: Time.current } }
options { { color: :blue } }
other_options { { Tag::MAGIC => :magic } }
self.end { Date.tomorrow }

trait :old do
published_at { 1.week.ago }
end
end
end
RUBY

include_examples 'autocorrect', bad, corrected
end