/
multiple_memoized_helpers_spec.rb
162 lines (143 loc) · 3.64 KB
/
multiple_memoized_helpers_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::RSpec::MultipleMemoizedHelpers, :config do
let(:cop_config) { { 'Max' => 1 } }
it 'flags excessive `#let`' do
expect_offense(<<~RUBY)
describe Foo do
^^^^^^^^^^^^^^^ Example group has too many memoized helpers [2/1]
let(:foo) { Foo.new }
let(:bar) { Bar.new }
end
RUBY
end
it 'flags excessive `#let!`' do
expect_offense(<<~RUBY)
describe Foo do
^^^^^^^^^^^^^^^ Example group has too many memoized helpers [2/1]
let(:foo) { Foo.new }
let!(:bar) { Bar.new }
end
RUBY
end
it 'ignores a reasonable number of memoized helpers' do
expect_no_offenses(<<~RUBY)
describe Foo do
let(:foo) { Foo.new }
end
RUBY
end
it 'ignores overridden `#let`' do
expect_no_offenses(<<~RUBY)
describe Foo do
let(:foo) { Foo.new }
context do
let(:foo) { Foo.new(1) }
context do
let(:foo) { Foo.new(1, 2) }
end
end
end
RUBY
end
it 'ignores `#subject`' do
expect_no_offenses(<<~RUBY)
describe Foo do
subject(:bar) { Bar.new }
let(:foo) { Foo.new }
end
RUBY
end
it 'ignores `#subject!`' do
expect_no_offenses(<<~RUBY)
describe Foo do
subject!(:bar) { Bar.new }
let(:foo) { Foo.new }
end
RUBY
end
it 'ignores `#subject` without a name' do
expect_no_offenses(<<~RUBY)
describe Foo do
subject { Foo.new }
let(:bar) { Bar.new }
end
RUBY
end
it 'flags nested `#let`' do
expect_offense(<<~RUBY)
describe Foo do
let(:foo) { Foo.new }
context 'when blah' do
^^^^^^^^^^^^^^^^^^^^^^ Example group has too many memoized helpers [2/1]
let(:bar) { Bar.new }
end
end
RUBY
end
it 'ignores distributed `#let`' do
expect_no_offenses(<<~RUBY)
describe Foo do
context 'when bloo' do
let(:foo) { Foo.new }
end
context 'when blah' do
let(:bar) { Bar.new }
end
end
RUBY
end
context 'when using AllowSubject configuration', :config do
let(:cop_config) { { 'Max' => 1, 'AllowSubject' => false } }
it 'flags `#subject` without name' do
expect_offense(<<~RUBY)
describe Foo do
^^^^^^^^^^^^^^^ Example group has too many memoized helpers [2/1]
subject { Foo.new }
let(:foo) { Foo.new }
end
RUBY
end
it 'flags `#subject`' do
expect_offense(<<~RUBY)
describe Foo do
^^^^^^^^^^^^^^^ Example group has too many memoized helpers [2/1]
subject(:foo) { Foo.new }
let(:bar) { Foo.new }
end
RUBY
end
it 'flags `#subject!`' do
expect_offense(<<~RUBY)
describe Foo do
^^^^^^^^^^^^^^^ Example group has too many memoized helpers [2/1]
subject!(:foo) { Foo.new }
let(:bar) { Foo.new }
end
RUBY
end
it 'ignores overridden subjects' do
expect_no_offenses(<<~RUBY)
describe Foo do
subject { Foo.new }
context do
subject { Foo.new(1) }
context do
subject { Foo.new(1, 2) }
end
end
end
RUBY
end
end
it 'support --auto-gen-config' do
inspect_source(<<-RUBY, 'spec/foo_spec.rb')
describe Foo do
let(:foo) { Foo.new }
let(:bar) { Bar.new }
let(:baz) { Baz.new }
let(:qux) { Qux.new }
end
RUBY
expect(cop.config_to_allow_offenses[:exclude_limit]).to eq('Max' => 4)
end
end