/
around_hooks.feature
343 lines (303 loc) · 9.12 KB
/
around_hooks.feature
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
Feature: around hooks
Around hooks receive the example as a block argument, extended to behave like
a proc. This lets you define code that should be executed before and after
the example. Of course, you can do the same thing with before and after hooks,
and it's often cleaner to do so.
Where around hooks shine is when you want to run an example in a block. For
example, if your database library offers a transaction method that receives
a block, you can use an around hook as described in the first scenario:
WARNING: around hooks do not share state with the example the way before and
after hooks do. This means that you can not share instance variables between
around hooks and examples.
Also, mock frameworks are set up and torn down within the context of running
the example, so you can not interact with them directly in around hooks.
Scenario: use the example as a proc within the block passed to around()
Given a file named "example_spec.rb" with:
"""
class Database
def self.transaction
puts "open transaction"
yield
puts "close transaction"
end
end
describe "around filter" do
around(:each) do |example|
Database.transaction(&example)
end
it "gets run in order" do
puts "run the example"
end
end
"""
When I run `rspec example_spec.rb`
Then the output should contain:
"""
open transaction
run the example
close transaction
"""
Scenario: invoke the example using run()
Given a file named "example_spec.rb" with:
"""
describe "around hook" do
around(:each) do |example|
puts "around each before"
example.run
puts "around each after"
end
it "gets run in order" do
puts "in the example"
end
end
"""
When I run `rspec example_spec.rb`
Then the output should contain:
"""
around each before
in the example
around each after
"""
Scenario: access the example metadata
Given a file named "example_spec.rb" with:
"""
describe "something" do
around(:each) do |example|
puts example.metadata[:foo]
example.run
end
it "does something", :foo => "this should show up in the output" do
end
end
"""
When I run `rspec example_spec.rb`
Then the output should contain "this should show up in the output"
Scenario: define a global around hook
Given a file named "example_spec.rb" with:
"""
RSpec.configure do |c|
c.around(:each) do |example|
puts "around each before"
example.run
puts "around each after"
end
end
describe "around filter" do
it "gets run in order" do
puts "in the example"
end
end
"""
When I run `rspec example_spec.rb`
Then the output should contain:
"""
around each before
in the example
around each after
"""
Scenario: before/after(:each) hooks are wrapped by the around hook
Given a file named "example_spec.rb" with:
"""
describe "around filter" do
around(:each) do |example|
puts "around each before"
example.run
puts "around each after"
end
before(:each) do
puts "before each"
end
after(:each) do
puts "after each"
end
it "gets run in order" do
puts "in the example"
end
end
"""
When I run `rspec example_spec.rb`
Then the output should contain:
"""
around each before
before each
in the example
after each
around each after
"""
Scenario: before/after(:all) hooks are NOT wrapped by the around hook
Given a file named "example_spec.rb" with:
"""
describe "around filter" do
around(:each) do |example|
puts "around each before"
example.run
puts "around each after"
end
before(:all) do
puts "before all"
end
after(:all) do
puts "after all"
end
it "gets run in order" do
puts "in the example"
end
end
"""
When I run `rspec --format progress example_spec.rb`
Then the output should contain:
"""
before all
around each before
in the example
around each after
.after all
"""
Scenario: examples run by an around block are run in the configured context
Given a file named "example_spec.rb" with:
"""
module IncludedInConfigureBlock
def included_in_configure_block; true; end
end
Rspec.configure do |c|
c.include IncludedInConfigureBlock
end
describe "around filter" do
around(:each) do |example|
example.run
end
it "runs the example in the correct context" do
included_in_configure_block.should be_true
end
end
"""
When I run `rspec example_spec.rb`
Then the output should contain "1 example, 0 failure"
Scenario: implicitly pending examples are detected as Not yet implemented
Given a file named "example_spec.rb" with:
"""
describe "implicit pending example" do
around(:each) do |example|
example.run
end
it "should be detected as Not yet implemented"
end
"""
When I run `rspec example_spec.rb`
Then the output should contain "1 example, 0 failures, 1 pending"
And the output should contain:
"""
Pending:
implicit pending example should be detected as Not yet implemented
# Not yet implemented
"""
Scenario: explicitly pending examples are detected as pending
Given a file named "example_spec.rb" with:
"""
describe "explicit pending example" do
around(:each) do |example|
example.run
end
it "should be detected as pending" do
pending
end
end
"""
When I run `rspec example_spec.rb`
Then the output should contain "1 example, 0 failures, 1 pending"
And the output should contain:
"""
explicit pending example should be detected as pending
# No reason given
"""
Scenario: multiple around hooks in the same scope
Given a file named "example_spec.rb" with:
"""
describe "if there are multiple around hooks in the same scope" do
around(:each) do |example|
puts "first around hook before"
example.run
puts "first around hook after"
end
around(:each) do |example|
puts "second around hook before"
example.run
puts "second around hook after"
end
it "they should all be run" do
puts "in the example"
1.should eq(1)
end
end
"""
When I run `rspec example_spec.rb`
Then the output should contain "1 example, 0 failure"
And the output should contain:
"""
first around hook before
second around hook before
in the example
second around hook after
first around hook after
"""
Scenario: around hooks in multiple scopes
Given a file named "example_spec.rb" with:
"""
describe "if there are around hooks in an outer scope" do
around(:each) do |example|
puts "first outermost around hook before"
example.run
puts "first outermost around hook after"
end
around(:each) do |example|
puts "second outermost around hook before"
example.run
puts "second outermost around hook after"
end
describe "outer scope" do
around(:each) do |example|
puts "first outer around hook before"
example.run
puts "first outer around hook after"
end
around(:each) do |example|
puts "second outer around hook before"
example.run
puts "second outer around hook after"
end
describe "inner scope" do
around(:each) do |example|
puts "first inner around hook before"
example.run
puts "first inner around hook after"
end
around(:each) do |example|
puts "second inner around hook before"
example.run
puts "second inner around hook after"
end
it "they should all be run" do
puts "in the example"
end
end
end
end
"""
When I run `rspec example_spec.rb`
Then the output should contain "1 example, 0 failure"
And the output should contain:
"""
first outermost around hook before
second outermost around hook before
first outer around hook before
second outer around hook before
first inner around hook before
second inner around hook before
in the example
second inner around hook after
first inner around hook after
second outer around hook after
first outer around hook after
second outermost around hook after
first outermost around hook after
"""