|
7 | 7 | factory :post do |
8 | 8 | title "Something" |
9 | 9 | ^^^^^^^^^^^^^^^^^ 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. |
10 | 18 | published_at 1.day.from_now |
11 | 19 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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. |
16 | 22 | update_times [Time.current] |
17 | 23 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values. |
18 | 24 | meta_tags(foo: Time.current) |
19 | 25 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 } } |
20 | 51 | end |
21 | 52 | end |
22 | 53 | RUBY |
|
35 | 66 | end |
36 | 67 | end |
37 | 68 | 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 |
38 | 80 | end |
39 | 81 |
|
40 | 82 | it 'registers an offense in a transient block' do |
|
50 | 92 | end |
51 | 93 | end |
52 | 94 | 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 |
53 | 106 | end |
54 | 107 |
|
55 | 108 | it 'registers an offense for an attribute defined on `self`' do |
|
62 | 115 | end |
63 | 116 | end |
64 | 117 | 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 |
65 | 127 | end |
66 | 128 |
|
67 | 129 | it 'registers an offense for attributes defined on explicit receiver' do |
|
77 | 139 | end |
78 | 140 | end |
79 | 141 | 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 |
80 | 153 | end |
81 | 154 |
|
82 | 155 | it 'accepts valid factory definitions' do |
|
161 | 234 | end |
162 | 235 | RUBY |
163 | 236 | 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 |
216 | 237 | end |
0 commit comments