/
empty_example_group_spec.rb
229 lines (199 loc) · 5.8 KB
/
empty_example_group_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::RSpec::EmptyExampleGroup do
it 'flags an empty example group' do
expect_offense(<<~RUBY)
describe Foo do
context 'when bar' do
^^^^^^^^^^^^^^^^^^ Empty example group detected.
let(:foo) { bar }
end
describe '#thingy?' do
specify do
expect(whatever.thingy?).to be(true)
end
end
it { should be_true }
end
RUBY
end
it 'flags an empty top level describe' do
expect_offense(<<~RUBY)
describe Foo do
^^^^^^^^^^^^ Empty example group detected.
end
RUBY
end
it 'flags example group with examples defined in hooks' do
expect_offense(<<~RUBY)
context 'hook with implicit scope' do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Empty example group detected.
before do
it { is_expected.to never_run }
end
end
context 'hook with explicit scope' do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Empty example group detected.
around(:example) do
it { is_expected.to never_run }
end
end
context 'hook with explicit scope and metadata' do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Empty example group detected.
after(:each, :corrupt, type: :cop) do
it { is_expected.to never_run }
end
end
RUBY
end
it 'ignores example group with examples defined in iterator' do
expect_no_offenses(<<~RUBY)
describe 'RuboCop monthly' do
[1, 2, 3].each do |page|
it { expect(newspaper(page)).to have_ads }
end
end
RUBY
end
it 'ignores example group with examples defined in an iterator' do
expect_no_offenses(<<~RUBY)
describe 'RuboCop weekly' do
some_method
[1, 2, 3].each do |page|
it { expect(newspaper(page)).to have_ads }
end
more_surroundings
end
RUBY
end
it 'flags example group with no examples defined in an iterator' do
expect_offense(<<~RUBY)
describe 'RuboCop Sunday' do
^^^^^^^^^^^^^^^^^^^^^^^^^ Empty example group detected.
some_method
[1, 2, 3].each do |page|
no_examples_here
and_no_ads_either
end
more_surroundings
end
RUBY
end
it 'ignores example group with examples defined in an custom block' do
expect_no_offenses(<<~RUBY)
context 'without arguments' do
mute_warnings do
it { expect(newspaper(page)).to have_a_lot_of_ads }
end
end
context 'with an argument' do
with_role :reader do
it { expect(newspaper(page)).to have_a_lot_of_ads }
end
end
context 'with a block argument' do
for_all_species_coming_from(:fish) do |creature|
it { expect(newspaper(page)).to have_a_lot_of_ads }
end
end
RUBY
end
it 'ignores example group with examples defined in an obscure iterators' do
expect_no_offenses(<<~RUBY)
describe 'RuboCop Friday night' do
context 'with each.with_object' do
[1, 2, 3].each.with_object(0) do |page, price|
it { expect(newspaper(page)).to have_ads }
end
end
context 'with each_with_index' do
[1, 2, 3].each_with_index do |page, index|
it { expect(newspaper(page)).to have_ads }
end
end
context 'with map' do
[1, 2, 3].map do |page, index|
it { expect(newspaper(page)).to have_ads }
end
end
context 'with count' do
[1, 2, 3].count do |page, index|
it { expect(newspaper(page)).to have_ads }
end
end
end
RUBY
end
it 'ignores example group with examples defined in a nested iterator' do
expect_no_offenses(<<~RUBY)
describe 'RuboCop daily' do
some_method
[1, 2, 3].each do |page|
some_method
[1, 2, 3].each do |paragraph|
it { expect(newspaper(page, paragraph)).to have_ads }
end
more_surroundings
end
more_surroundings
end
RUBY
end
it 'ignores examples groups with includes' do
expect_no_offenses(<<~RUBY)
describe Foo do
context "when something is true" do
include_examples "some expectations"
end
context "when something else is true" do
include_context "some expectations"
end
context "when the third thing is true" do
it_behaves_like "some thingy"
end
context "when the fourth thing is true" do
it_behaves_like "some thingy" do
let(:a) { 'foo' }
end
end
context "when the fifth thing is true" do
it_behaves_like "some thingy", &block
end
end
RUBY
end
it 'ignores methods matching example group names' do
expect_no_offenses(<<~RUBY)
describe Foo do
it 'yields a block when given' do
value = nil
helper.feature('whatevs') { value = 5 }
expect(value).to be 5
end
end
RUBY
end
it 'flags custom include methods by default' do
expect_offense(<<~RUBY)
describe Foo do
context "when I do something clever" do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Empty example group detected.
it_has_special_behavior
end
end
RUBY
end
context 'when a custom include method is specified' do
let(:cop_config) do
{ 'CustomIncludeMethods' => %w[it_has_special_behavior] }
end
it 'ignores an empty example group with a custom include' do
expect_no_offenses(<<~RUBY)
describe Foo do
context "when I do something clever" do
it_has_special_behavior
end
end
RUBY
end
end
end