Skip to content

Commit abe73f0

Browse files
authored
Merge pull request #1064 from rubocop-hq/refactor-a-spec
Refactor a spec
2 parents 5e23ee2 + 9ea14b9 commit abe73f0

File tree

2 files changed

+81
-56
lines changed

2 files changed

+81
-56
lines changed

.rubocop_todo.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@
1010
# Configuration parameters: CountComments, CountAsOne.
1111
Metrics/ClassLength:
1212
Max: 106
13+
14+
RSpec/ExampleLength:
15+
Exclude:
16+
- spec/rubocop/cop/rspec/factory_bot/attribute_defined_statically_spec.rb

spec/rubocop/cop/rspec/factory_bot/attribute_defined_statically_spec.rb

Lines changed: 77 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,47 @@
77
factory :post do
88
title "Something"
99
^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
10+
comments_count 0
11+
^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
12+
tag Tag::MAGIC
13+
^^^^^^^^^^^^^^ Use a block to declare attribute values.
14+
recent_statuses []
15+
^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
16+
status([:draft, :published].sample)
17+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
1018
published_at 1.day.from_now
1119
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
12-
status [:draft, :published].sample
13-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
14-
created_at 1.day.ago
15-
^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
20+
created_at(1.day.ago)
21+
^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
1622
update_times [Time.current]
1723
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
1824
meta_tags(foo: Time.current)
1925
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
26+
other_tags({ foo: Time.current })
27+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
28+
options color: :blue
29+
^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
30+
other_options Tag::MAGIC => :magic
31+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
32+
end
33+
end
34+
RUBY
35+
36+
expect_correction(<<-RUBY)
37+
FactoryBot.define do
38+
factory :post do
39+
title { "Something" }
40+
comments_count { 0 }
41+
tag { Tag::MAGIC }
42+
recent_statuses { [] }
43+
status { [:draft, :published].sample }
44+
published_at { 1.day.from_now }
45+
created_at { 1.day.ago }
46+
update_times { [Time.current] }
47+
meta_tags { { foo: Time.current } }
48+
other_tags { { foo: Time.current } }
49+
options { { color: :blue } }
50+
other_options { { Tag::MAGIC => :magic } }
2051
end
2152
end
2253
RUBY
@@ -35,6 +66,17 @@
3566
end
3667
end
3768
RUBY
69+
70+
expect_correction(<<-RUBY)
71+
FactoryBot.define do
72+
factory :post do
73+
trait :published do
74+
title { "Something" }
75+
published_at { 1.day.from_now }
76+
end
77+
end
78+
end
79+
RUBY
3880
end
3981

4082
it 'registers an offense in a transient block' do
@@ -50,6 +92,17 @@
5092
end
5193
end
5294
RUBY
95+
96+
expect_correction(<<-RUBY)
97+
FactoryBot.define do
98+
factory :post do
99+
transient do
100+
title { "Something" }
101+
published_at { 1.day.from_now }
102+
end
103+
end
104+
end
105+
RUBY
53106
end
54107

55108
it 'registers an offense for an attribute defined on `self`' do
@@ -62,6 +115,15 @@
62115
end
63116
end
64117
RUBY
118+
119+
expect_correction(<<-RUBY)
120+
FactoryBot.define do
121+
factory :post do
122+
self.start { Date.today }
123+
self.end { Date.tomorrow }
124+
end
125+
end
126+
RUBY
65127
end
66128

67129
it 'registers an offense for attributes defined on explicit receiver' do
@@ -77,6 +139,17 @@
77139
end
78140
end
79141
RUBY
142+
143+
expect_correction(<<-RUBY)
144+
FactoryBot.define do
145+
factory :post do |post_definition|
146+
post_definition.end { Date.tomorrow }
147+
post_definition.trait :published do |published_definition|
148+
published_definition.published_at { 1.day.from_now }
149+
end
150+
end
151+
end
152+
RUBY
80153
end
81154

82155
it 'accepts valid factory definitions' do
@@ -161,56 +234,4 @@
161234
end
162235
RUBY
163236
end
164-
165-
bad = <<-RUBY
166-
FactoryBot.define do
167-
factory :post do
168-
title "Something"
169-
comments_count 0
170-
tag Tag::MAGIC
171-
recent_statuses []
172-
status([:draft, :published].sample)
173-
published_at 1.day.from_now
174-
created_at(1.day.ago)
175-
updated_at Time.current
176-
update_times [Time.current]
177-
meta_tags(foo: Time.current)
178-
other_tags({ foo: Time.current })
179-
options color: :blue
180-
other_options Tag::MAGIC => :magic
181-
self.end Date.tomorrow
182-
183-
trait :old do
184-
published_at 1.week.ago
185-
end
186-
end
187-
end
188-
RUBY
189-
190-
corrected = <<-RUBY
191-
FactoryBot.define do
192-
factory :post do
193-
title { "Something" }
194-
comments_count { 0 }
195-
tag { Tag::MAGIC }
196-
recent_statuses { [] }
197-
status { [:draft, :published].sample }
198-
published_at { 1.day.from_now }
199-
created_at { 1.day.ago }
200-
updated_at { Time.current }
201-
update_times { [Time.current] }
202-
meta_tags { { foo: Time.current } }
203-
other_tags { { foo: Time.current } }
204-
options { { color: :blue } }
205-
other_options { { Tag::MAGIC => :magic } }
206-
self.end { Date.tomorrow }
207-
208-
trait :old do
209-
published_at { 1.week.ago }
210-
end
211-
end
212-
end
213-
RUBY
214-
215-
include_examples 'autocorrect', bad, corrected
216237
end

0 commit comments

Comments
 (0)