-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
create_list_spec.rb
169 lines (139 loc) · 4.6 KB
/
create_list_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::RSpec::FactoryBot::CreateList, :config do
let(:cop_config) do
{ 'EnforcedStyle' => enforced_style }
end
context 'when EnforcedStyle is :create_list' do
let(:enforced_style) { :create_list }
it 'flags usage of n.times with no arguments' do
expect_offense(<<-RUBY)
3.times { create :user }
^^^^^^^ Prefer create_list.
RUBY
end
it 'flags usage of n.times when FactoryGirl.create is used' do
expect_offense(<<-RUBY)
3.times { FactoryGirl.create :user }
^^^^^^^ Prefer create_list.
RUBY
end
it 'flags usage of n.times when FactoryBot.create is used' do
expect_offense(<<-RUBY)
3.times { FactoryBot.create :user }
^^^^^^^ Prefer create_list.
RUBY
end
it 'ignores create method of other object' do
expect_no_offenses(<<-RUBY)
3.times { SomeFactory.create :user }
RUBY
end
it 'ignores create in other block' do
expect_no_offenses(<<-RUBY)
allow(User).to receive(:create) { create :user }
RUBY
end
it 'ignores n.times with argument' do
expect_no_offenses(<<-RUBY)
3.times { |n| create :user, created_at: n.days.ago }
RUBY
end
it 'ignores n.times when there is no create call inside' do
expect_no_offenses(<<-RUBY)
3.times { do_something }
RUBY
end
it 'ignores n.times when there is other calls but create' do
expect_no_offenses(<<-RUBY)
used_passwords = []
3.times do
u = create :user
expect(used_passwords).not_to include(u.password)
used_passwords << u.password
end
RUBY
end
it 'flags FactoryGirl.create calls with a block' do
expect_offense(<<-RUBY)
3.times do
^^^^^^^ Prefer create_list.
create(:user) { |user| create :account, user: user }
end
RUBY
end
include_examples 'autocorrect',
'5.times { create :user }',
'create_list :user, 5'
include_examples 'autocorrect',
'5.times { create(:user, :trait) }',
'create_list(:user, 5, :trait)'
include_examples 'autocorrect',
'5.times { create :user, :trait, key: val }',
'create_list :user, 5, :trait, key: val'
include_examples 'autocorrect',
'5.times { FactoryGirl.create :user }',
'FactoryGirl.create_list :user, 5'
bad_code = <<-RUBY
3.times do
create(:user, :trait) { |user| create :account, user: user }
end
RUBY
good_code = <<-RUBY
create_list(:user, 3, :trait) { |user| create :account, user: user }
RUBY
include_examples 'autocorrect', bad_code, good_code
bad_code = <<-RUBY
3.times do
create(:user, :trait) do |user|
create :account, user: user
create :profile, user: user
end
end
RUBY
good_code = <<-RUBY
create_list(:user, 3, :trait) do |user|
create :account, user: user
create :profile, user: user
end
RUBY
include_examples 'autocorrect', bad_code, good_code
end
context 'when EnforcedStyle is :n_times' do
let(:enforced_style) { :n_times }
it 'flags usage of create_list' do
expect_offense(<<-RUBY)
create_list :user, 3
^^^^^^^^^^^ Prefer 3.times.
RUBY
end
it 'flags usage of FactoryGirl.create_list' do
expect_offense(<<-RUBY)
FactoryGirl.create_list :user, 3
^^^^^^^^^^^ Prefer 3.times.
RUBY
end
it 'flags usage of FactoryGirl.create_list with a block' do
expect_offense(<<-RUBY)
FactoryGirl.create_list(:user, 3) { |user| user.points = rand(1000) }
^^^^^^^^^^^ Prefer 3.times.
RUBY
end
it 'ignores create method of other object' do
expect_no_offenses(<<-RUBY)
SomeFactory.create_list :user, 3
RUBY
end
include_examples 'autocorrect',
'create_list :user, 5',
'5.times { create :user }'
include_examples 'autocorrect',
'create_list(:user, 5, :trait)',
'5.times { create(:user, :trait) }'
include_examples 'autocorrect',
'create_list :user, 5, :trait, key: val',
'5.times { create :user, :trait, key: val }'
include_examples 'autocorrect',
'FactoryGirl.create_list :user, 5',
'5.times { FactoryGirl.create :user }'
end
end