-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
multiple_expectations_spec.rb
259 lines (235 loc) · 7.18 KB
/
multiple_expectations_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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::RSpec::MultipleExpectations, :config do
context 'without configuration' do
let(:cop_config) { {} }
it 'flags multiple expectations' do
expect_offense(<<-RUBY)
describe Foo do
it 'uses expect twice' do
^^^^^^^^^^^^^^^^^^^^^^ Example has too many expectations [2/1].
expect(foo).to eq(bar)
expect(baz).to eq(bar)
end
end
RUBY
end
it 'approves of one expectation per example' do
expect_no_offenses(<<-RUBY)
describe Foo do
it 'does something neat' do
expect(neat).to be(true)
end
it 'does something cool' do
expect(cool).to be(true)
end
end
RUBY
end
it 'flags multiple expect_any_instance_of' do
expect_offense(<<-RUBY)
describe Foo do
it 'uses expect_any_instance_of twice' do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Example has too many expectations [2/1].
expect_any_instance_of(Foo).to receive(:bar)
expect_any_instance_of(Foo).to receive(:baz)
end
end
RUBY
end
it 'flags multiple is_expected' do
expect_offense(<<-RUBY)
describe Foo do
it 'uses expect_any_instance_of twice' do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Example has too many expectations [2/1].
is_expected.to receive(:bar)
is_expected.to receive(:baz)
end
end
RUBY
end
it 'flags multiple expects with blocks' do
expect_offense(<<-RUBY)
describe Foo do
it 'uses expect with block twice' do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Example has too many expectations [2/1].
expect { something }.to change(Foo.count)
expect { something }.to change(Bar.count)
end
end
RUBY
end
it 'counts aggregate_failures as one expectation' do
expect_no_offenses(<<-RUBY)
describe Foo do
it 'aggregates failures' do
aggregate_failures do
expect(foo).to eq(bar)
expect(baz).to eq(bar)
end
end
end
RUBY
end
it 'counts every aggregate_failures as an expectation' do
expect_offense(<<-RUBY)
describe Foo do
it 'has multiple aggregate_failures calls' do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Example has too many expectations [2/1].
aggregate_failures do
end
aggregate_failures do
end
end
end
RUBY
end
end
context 'with metadata' do
it 'ignores examples with `:aggregate_failures`' do
expect_no_offenses(<<-RUBY)
describe Foo do
it 'uses expect twice', :foo, :aggregate_failures do
expect(foo).to eq(bar)
expect(baz).to eq(bar)
end
end
RUBY
end
it 'ignores example groups with `:aggregate_failures`' do
expect_no_offenses(<<-RUBY)
describe Foo, :foo, :aggregate_failures do
it 'uses expect twice' do
expect(foo).to eq(bar)
expect(baz).to eq(bar)
end
end
RUBY
end
it 'ignores examples with `aggregate_failures: true`' do
expect_no_offenses(<<-RUBY)
describe Foo do
it 'uses expect twice', :foo, bar: 1, aggregate_failures: true do
expect(foo).to eq(bar)
expect(baz).to eq(bar)
end
end
RUBY
end
it 'ignores example groups with `aggregate_failures: true`' do
expect_no_offenses(<<-RUBY)
describe Foo, :foo, bar: 1, aggregate_failures: true do
it 'uses expect twice' do
expect(foo).to eq(bar)
expect(baz).to eq(bar)
end
end
RUBY
end
it 'prefers example metadata over example group metadata' do
expect_offense(<<-RUBY)
describe Foo, aggregate_failures: true do
it 'uses expect twice', aggregate_failures: false do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Example has too many expectations [2/1].
expect(foo).to eq(bar)
expect(baz).to eq(bar)
end
end
RUBY
end
it 'checks examples with `aggregate_failures: false`' do
expect_offense(<<-RUBY)
describe Foo do
it 'uses expect twice', aggregate_failures: false do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Example has too many expectations [2/1].
expect(foo).to eq(bar)
expect(baz).to eq(bar)
end
end
RUBY
end
it 'checks example groups with `aggregate_failures: false`' do
expect_offense(<<-RUBY)
describe Foo, aggregate_failures: false do
it 'uses expect twice' do
^^^^^^^^^^^^^^^^^^^^^^ Example has too many expectations [2/1].
expect(foo).to eq(bar)
expect(baz).to eq(bar)
end
end
RUBY
end
it 'checks examples with siblings with `aggregate_failures: true`' do
expect_offense(<<-RUBY)
describe Foo do
it 'uses expect twice' do
^^^^^^^^^^^^^^^^^^^^^^ Example has too many expectations [2/1].
expect(foo).to eq(bar)
expect(baz).to eq(bar)
end
it 'with aggregate_failures', aggregate_failures: true do
expect(foo).to eq(bar)
expect(baz).to eq(bar)
end
end
RUBY
end
it 'ignores examples with `aggregate_failures: true` defined deeply' do
expect_no_offenses(<<-RUBY)
describe Bar, aggregate_failures: true do
describe Foo do
it 'uses expect twice' do
expect(foo).to eq(bar)
expect(baz).to eq(bar)
end
it 'with aggregate_failures', aggregate_failures: false do
expect(foo).to eq(bar)
end
end
end
RUBY
end
end
context 'with Max configuration' do
let(:cop_config) do
{ 'Max' => '2' }
end
it 'permits two expectations' do
expect_no_offenses(<<-RUBY)
describe Foo do
it 'uses expect twice' do
expect(foo).to eq(bar)
expect(baz).to eq(bar)
end
end
RUBY
end
it 'flags three expectations' do
expect_offense(<<-RUBY)
describe Foo do
it 'uses expect three times' do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Example has too many expectations [3/2].
expect(foo).to eq(bar)
expect(baz).to eq(bar)
expect(qux).to eq(bar)
end
end
RUBY
end
end
it 'generates a todo based on the worst violation' do
inspect_source(<<-RUBY, 'spec/foo_spec.rb')
describe Foo do
it 'uses expect twice' do
expect(foo).to eq(bar)
expect(baz).to eq(bar)
end
it 'uses expect three times' do
expect(foo).to eq(bar)
expect(baz).to eq(bar)
expect(qux).to eq(bar)
end
end
RUBY
expect(cop.config_to_allow_offenses[:exclude_limit]).to eq('Max' => 3)
end
end