/
test_case_spec.rb
403 lines (342 loc) · 11.5 KB
/
test_case_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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
describe RSpec::Core::ExampleGroup do
it "supports using assertions in examples" do
lambda {assert_equal 1, 1}.should_not raise_error
end
end
describe "RSpec::Unit::TestCase" do
before do
@foo = Class.new(RSpec::Unit::TestCase)
@foo_definition_line = __LINE__ - 1
@caller_at_foo_definition = caller
@formatter = RSpec::Core::Formatters::BaseFormatter.new('')
end
describe "identifying test methods" do
it "ignores methods that don't begin with 'test_'" do
@foo.class_eval do
def bar; end
end
@foo.examples.should be_empty
end
it "notices methods that begin with 'test_'" do
@foo.class_eval do
def test_bar; end
end
@foo.examples.size.should == 1
@foo.examples.first.metadata[:description].should == 'test_bar'
end
it "ignores non-public test methods" do
@foo.class_eval do
protected
def test_foo; end
private
def test_bar; end
end
@foo.examples.should be_empty
end
it "ignores methods with good names but requiring parameters" do
@foo.class_eval do
def test_foo(a); end
def test_bar(a, *b); end
end
@foo.examples.should be_empty
end
it "notices methods that have only optional parameters" do
@foo.class_eval do
def test_foo(*a); end
end
@foo.examples.size.should == 1
@foo.examples.first.metadata[:description].should == 'test_foo'
end
it "creates an example to represent a test method" do
@foo.class_eval do
def test_bar; end
end
@foo.examples.size.should == 1
@foo.examples.first.metadata[:description].should == 'test_bar'
end
it "creates examples for inherited methods" do
@foo.class_eval do
def test_bar; end
end
bar = Class.new(@foo)
bar.examples.size.should == 1
bar.examples.first.metadata[:description].should == 'test_bar'
end
it "creates examples for methods newly added to superclasses" do
bar = Class.new(@foo)
@foo.class_eval do
def test_bar; end
end
bar.examples.size.should == 1
bar.examples.first.metadata[:description].should == 'test_bar'
end
it "creates examples for methods added by inclusion of a module" do
bar = Module.new do
def test_bar; end
end
@foo.send(:include, bar)
@foo.examples.size.should == 1
@foo.examples.first.metadata[:description].should == 'test_bar'
end
end
describe "running test methods" do
it "runs the test methods as examples" do
@foo.class_eval do
def test_bar; end
end
eg_inst = mock_example_group_instance(@foo)
eg_inst.should_receive(:test_bar).once
@foo.run_all
end
it "brackets test methods with setup/teardown" do
@foo.class_eval do
def test_bar; end
def test_baz; end
end
eg_inst = mock_example_group_instance(@foo)
eg_inst.should_receive(:setup) .once.ordered
eg_inst.should_receive(:test_bar).once.ordered
eg_inst.should_receive(:teardown).once.ordered
eg_inst.should_receive(:setup) .once.ordered
eg_inst.should_receive(:test_baz).once.ordered
eg_inst.should_receive(:teardown).once.ordered
@foo.run_all
end
it "only calls setup/teardown once per test in subclasses" do
@foo.class_eval do
def test_baz; end
end
bar = Class.new(@foo)
bar.class_eval do
def test_quux; end
end
eg_inst = mock_example_group_instance(bar)
eg_inst.should_receive(:setup) .once.ordered
eg_inst.should_receive(:test_baz) .once.ordered
eg_inst.should_receive(:teardown) .once.ordered
eg_inst.should_receive(:setup) .once.ordered
eg_inst.should_receive(:test_quux).once.ordered
eg_inst.should_receive(:teardown) .once.ordered
bar.run_all
end
it "records failed tests in RSpec style" do
@foo.class_eval do
def test_bar; flunk; end
end
@foo.run_all(@formatter)
@formatter.failed_examples.size.should == 1
end
it "indicates failed tests in test/unit style" do
@foo.class_eval do
class <<self; attr_accessor :_passed; end
def test_bar; flunk; end
def teardown; self.class._passed = passed?; end
end
@foo.run_all
@foo._passed.should == false
end
it "records passed tests in RSpec style" do
@foo.class_eval do
def test_bar; assert true; end
end
@foo.run_all(@formatter)
@formatter.failed_examples.should be_empty
end
it "indicates passed tests in test/unit style" do
@foo.class_eval do
class <<self; attr_accessor :_passed; end
def test_bar; assert true; end
def teardown; self.class._passed = passed?; end
end
@foo.run_all
@foo._passed.should == true
end
end
describe "inherited" do
it "adds the new subclass to RSpec.world.example_groups" do
class SampleTestCase < RSpec::Unit::TestCase
end
RSpec.world.example_groups.should == [@foo, SampleTestCase]
end
end
describe "ancestors" do
before do
@bar = Class.new(@foo)
end
it "removes TestCase from the end" do
@bar.ancestors.should == [@bar, @foo]
end
end
describe "test class metadata" do
before do
class SampleTestCaseForName < RSpec::Unit::TestCase
end
end
it "sets :description to the class name if the class has a name" do
SampleTestCaseForName.metadata[:example_group][:description].should == "SampleTestCaseForName"
end
it "sets :description to '<Anonymous TestCase>' for anonymous test classes" do
@foo.metadata[:example_group][:description].should == "<Anonymous TestCase>"
end
it "adds :test_unit => true" do
@foo.metadata[:example_group][:test_unit].should be_true
end
it "sets :file_path to the file in which the class is first defined" do
@foo.metadata[:example_group][:file_path].should == __FILE__
end
it "sets :line_number to the line where the class definition begins" do
@foo.metadata[:example_group][:line_number].should == @foo_definition_line
end
it "sets :location to file_path and line_number" do
@foo.metadata[:example_group][:location].should == "#{__FILE__}:#{@foo_definition_line}"
end
it "sets :caller" do
@foo.metadata[:example_group][:caller].first.should =~ Regexp.new(Regexp.escape(@foo.metadata[:example_group][:location]))
@foo.metadata[:example_group][:caller].size.should == @caller_at_foo_definition.size + 3
end
it "has nil for :block and :describes" do
@foo.metadata[:example_group][:block].should be_nil
@foo.metadata[:example_group][:describes].should be_nil
end
it "records test_case_info metadata" do
@foo.class_eval do
test_case_info :foo => :bar
end
@foo.metadata[:example_group][:foo].should == :bar
end
end
describe "test method metadata" do
def find_example(example_group, name)
example_group.examples.find{|e|e.description == name}
end
def test_baz_metadata
find_example(@foo, 'test_baz').metadata
end
it "uses a test method's name as its :description" do
@foo.class_eval do
def test_baz; end
end
@foo.examples.first.metadata[:description].should == 'test_baz'
end
it "sets the test method's :full_description to ClassName#method_name" do
@foo.class_eval do
def test_baz; end
end
test_baz_metadata[:full_description].should == "#{@foo.metadata[:example_group][:description]}#test_baz"
end
it "adds :test_unit => true" do
@foo.class_eval do
def test_baz; end
end
test_baz_metadata[:test_unit].should be_true
end
it "sets :file_path to the file where the method is defined" do
@foo.class_eval do
def test_baz; end
end
test_baz_metadata[:file_path].should == __FILE__
end
it "sets :line_number to the line where the method definition begins" do
@foo.class_eval do
def test_baz
end
end
test_baz_metadata[:line_number].should == (__LINE__ - 3)
end
it "sets :location to file path and line number" do
@foo.class_eval do
def test_baz; end
end
test_baz_metadata[:location].should == "#{__FILE__}:#{__LINE__-2}"
end
it "sets :example_group and :behaviour to the test case class's metadata" do
@foo.class_eval do
def test_baz; end
end
test_baz_metadata[:example_group].should == @foo.metadata[:example_group]
test_baz_metadata[:behaviour].should == @foo.metadata[:example_group]
end
it "sets :caller" do
@foo.class_eval do
def test_baz; end
end
test_baz_metadata[:caller].first.should match(/^#{Regexp.escape(@foo.examples.first.metadata[:location])}/)
test_baz_metadata[:caller].size.should == caller.size + 3
end
it "records test_info metadata for next test method" do
@foo.class_eval do
test_info :foo => :bar
def test_baz; end
end
test_baz_metadata[:foo].should == :bar
end
it "records test_info metadata *only* for next test method" do
@foo.class_eval do
test_info :foo => :bar
def test_baz; end
def test_quux; end
end
find_example(@foo, 'test_quux').metadata[:foo].should be_nil
end
end
describe "examples within a test case" do
it "allows 'example' to create an example" do
@foo.class_eval do
example "should bar" do end
end
@foo.examples.size.should == 1
@foo.examples.first.metadata[:description].should == 'should bar'
end
it "supports 'test' as an alias of example" do
@foo.class_eval do
test "should bar" do end
end
@foo.examples.size.should == 1
@foo.examples.first.metadata[:description].should == 'should bar'
@foo.examples.first.metadata[:test_unit].should be_true
end
it "heeds 'alias_example_to'" do
@foo.class_eval do
alias_example_to :make_test
make_test "should bar" do end
end
@foo.examples.size.should == 1
@foo.examples.first.metadata[:description].should == 'should bar'
end
it "allows defining 'before' blocks" do
@foo.class_eval do
before {bar}
def test_bar; end
end
eg_inst = mock_example_group_instance(@foo)
eg_inst.should_receive(:bar).once
@foo.run_all
end
it "allows defining 'after' blocks" do
@foo.class_eval do
after {bar}
def test_bar; end
end
eg_inst = mock_example_group_instance(@foo)
eg_inst.should_receive(:bar).once
@foo.run_all
end
it "allows examples to use instance variables created in 'setup'" do
@foo.class_eval do
def setup; super; @quux = 42; end
it "quux" do @quux.should == 42 end
end
@foo.run_all(@formatter)
@formatter.failed_examples.should be_empty
end
it "allows test methods to use instance variables created in 'before' blocks" do
@foo.class_eval do
before { @quux = 42 }
def test_quux; assert_equal 42, @quux; end
end
@foo.run_all(@formatter)
@formatter.failed_examples.should be_empty
end
end
end