Fingertips-on-test-spec 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,467 @@
1
+ require 'rubygems' rescue LoadError
2
+ require 'test/spec'
3
+ require 'mocha'
4
+
5
+ require 'active_support'
6
+ require 'active_support/testing/assertions'
7
+
8
+ require File.expand_path('../../lib/test/spec/rails/expectations', __FILE__)
9
+
10
+ module TestingAssertionsThemselves
11
+ class << self
12
+ attr_accessor :assertions
13
+ end
14
+ @assertions = []
15
+
16
+ def assert(*args)
17
+ TestingAssertionsThemselves.assertions << [:assert, args]
18
+ end
19
+
20
+ def assert_equal(*args)
21
+ TestingAssertionsThemselves.assertions << [:assert_equal, args]
22
+ end
23
+
24
+ def self.last_assertion
25
+ TestingAssertionsThemselves.assertions.last
26
+ end
27
+ end
28
+
29
+ class Test::Spec::Should
30
+ include TestingAssertionsThemselves
31
+ end
32
+
33
+ class Test::Spec::ShouldNot
34
+ include TestingAssertionsThemselves
35
+ end
36
+
37
+ module AssertionAssertions
38
+ def assert_assert_success(message=nil)
39
+ assertion = TestingAssertionsThemselves.last_assertion
40
+
41
+ assert_equal :assert, assertion[0]
42
+ assert assertion[1][0]
43
+ assert_equal(message, assertion[2][1]) if message
44
+ end
45
+
46
+ def assert_assert_failure(message=nil)
47
+ assertion = TestingAssertionsThemselves.last_assertion
48
+
49
+ assert_equal :assert, assertion[0]
50
+ assert !assertion[1][0]
51
+ assert_equal(message, assertion[1][1]) if message
52
+ end
53
+
54
+ def assert_assert_equal_success(message=nil)
55
+ assertion = TestingAssertionsThemselves.last_assertion
56
+
57
+ assert_equal :assert_equal, assertion[0]
58
+ assert_equal assertion[1][0], assertion[1][1]
59
+ assert_equal(message, assertion[1][2]) if message
60
+ end
61
+
62
+ def assert_assert_equal_failure(message=nil)
63
+ assertion = TestingAssertionsThemselves.last_assertion
64
+
65
+ assert_equal :assert_equal, assertion[0]
66
+ assert_not_equal assertion[1][0], assertion[1][1]
67
+ assert_equal(message, assertion[1][2]) if message
68
+ end
69
+ end
70
+
71
+ class Pony
72
+ class << self
73
+ attr_accessor :count
74
+ end
75
+ end
76
+
77
+ describe "Differ expectations" do
78
+ include AssertionAssertions
79
+
80
+ before do
81
+ TestingAssertionsThemselves.assertions = []
82
+ end
83
+
84
+ it "should succeed when the expected difference occurs on a local variable" do
85
+ count = 1
86
+ lambda {
87
+ count = 2
88
+ }.should.differ('count')
89
+ assert_assert_equal_success
90
+ end
91
+
92
+ it "should succeed when the expected difference occurs on an instance variable in the current scope" do
93
+ @count = 1
94
+ lambda {
95
+ @count = 2
96
+ }.should.differ('@count')
97
+ assert_assert_equal_success
98
+ end
99
+
100
+ it "should succeed when the expected difference occurs on a class in the current scope" do
101
+ Pony.count = 1
102
+ lambda {
103
+ Pony.count = 2
104
+ }.should.differ('Pony.count')
105
+ assert_assert_equal_success
106
+ end
107
+
108
+ it "should succeed when the difference is explicitly stated" do
109
+ Pony.count = 1
110
+ lambda {
111
+ Pony.count = 3
112
+ }.should.differ('Pony.count', +2)
113
+ assert_assert_equal_success
114
+ end
115
+
116
+ it "should fail when the expected difference does not occur" do
117
+ Pony.count = 1
118
+ lambda {
119
+ Pony.count = 3
120
+ }.should.differ('Pony.count')
121
+ assert_assert_equal_failure('"Pony.count" didn\'t change by 1')
122
+ end
123
+
124
+ it "should fail when the explicitly stated difference does not occur" do
125
+ Pony.count = 1
126
+ lambda {
127
+ Pony.count = 3
128
+ }.should.differ('Pony.count', +5)
129
+ assert_assert_equal_failure('"Pony.count" didn\'t change by 5')
130
+ end
131
+
132
+ it "should return the return value of the block" do
133
+ Pony.count = 3
134
+ result = lambda {
135
+ Pony.count += 1
136
+ }.should.differ('Pony.count', 1)
137
+ assert_equal 4, result, "differ didn't return the block result"
138
+ end
139
+
140
+ it "should succeed when multiple expected differences occur" do
141
+ count = 1
142
+ Pony.count = 1
143
+ lambda {
144
+ count = 2
145
+ Pony.count = 2
146
+ }.should.differ('Pony.count', +1, 'count', +1)
147
+
148
+ TestingAssertionsThemselves.assertions.each do |assertion, args|
149
+ assert_equal :assert_equal, assertion
150
+ assert_equal args[0], args[1]
151
+ end
152
+ end
153
+
154
+ it "should fail when first of the expected differences does not occur" do
155
+ count = 1
156
+ Pony.count = 1
157
+ lambda {
158
+ count += 4
159
+ }.should.differ('Pony.count', +2, 'count', +4)
160
+
161
+ assertion, args = TestingAssertionsThemselves.assertions.first
162
+ assert_equal :assert_equal, assertion
163
+ assert_not_equal(args[0], args[1])
164
+ assert_equal '"Pony.count" didn\'t change by 2', args[2]
165
+
166
+ assertion, args = TestingAssertionsThemselves.assertions.second
167
+ assert_equal :assert_equal, assertion
168
+ assert_equal(args[0], args[1])
169
+ end
170
+
171
+ it "should fail when second of the expected differences does not occur" do
172
+ count = 1
173
+ Pony.count = 1
174
+ lambda {
175
+ Pony.count += 2
176
+ }.should.differ('Pony.count', +2, 'count', +4)
177
+
178
+ assertion, args = TestingAssertionsThemselves.assertions.first
179
+ assert_equal :assert_equal, assertion
180
+ assert_equal(args[0], args[1])
181
+
182
+ assertion, args = TestingAssertionsThemselves.assertions.second
183
+ assert_equal :assert_equal, assertion
184
+ assert_not_equal(args[0], args[1])
185
+ assert_equal '"count" didn\'t change by 4', args[2]
186
+ end
187
+ end
188
+
189
+ describe "NotDiffer expectations" do
190
+ include AssertionAssertions
191
+
192
+ before do
193
+ TestingAssertionsThemselves.assertions = []
194
+ end
195
+
196
+ it "should succeed when no difference occurs on a local variable" do
197
+ count = 1
198
+ lambda {
199
+ }.should.not.differ('count')
200
+ assert_assert_equal_success
201
+ end
202
+
203
+ it "should succeed when no difference occurs on an instance variable in the current scope" do
204
+ @count = 1
205
+ lambda {
206
+ }.should.not.differ('@count')
207
+ assert_assert_equal_success
208
+ end
209
+
210
+ it "should succeed when no difference occurs on a class in the current scope" do
211
+ Pony.count = 1
212
+ lambda {
213
+ }.should.not.differ('Pony.count')
214
+ assert_assert_equal_success
215
+ end
216
+
217
+ it "should fail when a difference occurs" do
218
+ Pony.count = 1
219
+ lambda {
220
+ Pony.count = 3
221
+ }.should.not.differ('Pony.count')
222
+ assert_assert_equal_failure('"Pony.count" changed by 2, expected no change')
223
+ end
224
+
225
+ it "should return the return value of the block" do
226
+ Pony.count = 3
227
+ result = lambda {
228
+ Pony.count += 1
229
+ }.should.not.differ('Pony.count')
230
+ assert_equal 4, result, "differ didn't return the block result"
231
+ end
232
+
233
+ it "should succeed when multiple expected differences do not occur" do
234
+ count = 1
235
+ Pony.count = 1
236
+ lambda {
237
+ }.should.not.differ('Pony.count', 'count')
238
+
239
+ TestingAssertionsThemselves.assertions.each do |assertion, args|
240
+ assert_equal :assert_equal, assertion
241
+ assert_equal args[0], args[1]
242
+ end
243
+ end
244
+
245
+ it "should fail when first of the expected differences does occur" do
246
+ count = 1
247
+ Pony.count = 1
248
+ lambda {
249
+ Pony.count += 2
250
+ }.should.not.differ('Pony.count', 'count')
251
+
252
+ assertion, args = TestingAssertionsThemselves.assertions.first
253
+ assert_equal :assert_equal, assertion
254
+ assert_not_equal(args[0], args[1])
255
+ assert_equal '"Pony.count" changed by 2, expected no change', args[2]
256
+
257
+ assertion, args = TestingAssertionsThemselves.assertions.second
258
+ assert_equal :assert_equal, assertion
259
+ assert_equal(args[0], args[1])
260
+ end
261
+
262
+ it "should fail when second of the expected differences does occur" do
263
+ count = 1
264
+ Pony.count = 1
265
+ lambda {
266
+ count += 4
267
+ }.should.not.differ('Pony.count', 'count')
268
+
269
+ assertion, args = TestingAssertionsThemselves.assertions.first
270
+ assert_equal :assert_equal, assertion
271
+ assert_equal(args[0], args[1])
272
+
273
+ assertion, args = TestingAssertionsThemselves.assertions.second
274
+ assert_equal :assert_equal, assertion
275
+ assert_not_equal(args[0], args[1])
276
+ assert_equal '"count" changed by 4, expected no change', args[2]
277
+ end
278
+ end
279
+
280
+
281
+ describe "Record expectations" do
282
+ include AssertionAssertions
283
+
284
+ before do
285
+ TestingAssertionsThemselves.assertions = []
286
+ end
287
+
288
+ it "should succeed when equal_set assertions are correct" do
289
+ [].should.equal_set []
290
+ assert_assert_success
291
+
292
+ [stub(:id => 1)].should.equal_set [stub(:id => 1)]
293
+ assert_assert_success
294
+
295
+ [stub(:id => 1), stub(:id => 1)].should.equal_set [stub(:id => 1), stub(:id => 1)]
296
+ assert_assert_success
297
+
298
+ [stub(:id => 1), stub(:id => 2)].should.equal_set [stub(:id => 1), stub(:id => 2)]
299
+ assert_assert_success
300
+
301
+ [stub(:id => 2)].should.not.equal_set [stub(:id => 1)]
302
+ assert_assert_success
303
+
304
+ [stub(:id => 1), stub(:id => 2)].should.not.equal_set [stub(:id => 1)]
305
+ assert_assert_success
306
+
307
+ [stub(:id => 1)].should.not.equal_set [stub(:id => 1), stub(:id => 2)]
308
+ assert_assert_success
309
+
310
+ # equal_set ignores order
311
+ [stub(:id => 1), stub(:id => 2)].should.equal_set [stub(:id => 2), stub(:id => 1)]
312
+ assert_assert_success
313
+ end
314
+
315
+ it "should fail when equal_set assertions are not correct" do
316
+ [].should.not.equal_set []
317
+ assert_assert_failure("[] has the same records as []")
318
+
319
+ [stub(:id => 1), stub(:id => 1)].should.equal_set [stub(:id => 1)]
320
+ assert_assert_failure("[Mocha::Mock[1], Mocha::Mock[1]] does not have the same records as [Mocha::Mock[1]]")
321
+
322
+ [stub(:id => 1), stub(:id => 2)].should.equal_set [stub(:id => 1), stub(:id => 1), stub(:id => 2)]
323
+ assert_assert_failure("[Mocha::Mock[1], Mocha::Mock[2]] does not have the same records as [Mocha::Mock[1], Mocha::Mock[1], Mocha::Mock[2]]")
324
+
325
+ [stub(:id => 1), stub(:id => 2), stub(:id => 1)].should.equal_set [stub(:id => 1), stub(:id => 2), stub(:id => 2)]
326
+ assert_assert_failure("[Mocha::Mock[1], Mocha::Mock[2], Mocha::Mock[1]] does not have the same records as [Mocha::Mock[1], Mocha::Mock[2], Mocha::Mock[2]]")
327
+
328
+ [stub(:id => 1)].should.not.equal_set [stub(:id => 1)]
329
+ assert_assert_failure("[Mocha::Mock[1]] has the same records as [Mocha::Mock[1]]")
330
+
331
+ [stub(:id => 1), stub(:id => 2)].should.not.equal_set [stub(:id => 1), stub(:id => 2)]
332
+ assert_assert_failure("[Mocha::Mock[1], Mocha::Mock[2]] has the same records as [Mocha::Mock[1], Mocha::Mock[2]]")
333
+
334
+ [stub(:id => 2)].should.equal_set [stub(:id => 1)]
335
+ assert_assert_failure("[Mocha::Mock[2]] does not have the same records as [Mocha::Mock[1]]")
336
+
337
+ [stub(:id => 1), stub(:id => 2)].should.equal_set [stub(:id => 1)]
338
+ assert_assert_failure("[Mocha::Mock[1], Mocha::Mock[2]] does not have the same records as [Mocha::Mock[1]]")
339
+
340
+ [stub(:id => 1)].should.equal_set [stub(:id => 1), stub(:id => 2)]
341
+ assert_assert_failure("[Mocha::Mock[1]] does not have the same records as [Mocha::Mock[1], Mocha::Mock[2]]")
342
+ end
343
+
344
+ it "should succeed when equal_list assertions are correct" do
345
+ [].should.equal_list []
346
+ assert_assert_success
347
+
348
+ [stub(:id => 1)].should.equal_list [stub(:id => 1)]
349
+ assert_assert_success
350
+
351
+ [stub(:id => 1), stub(:id => 1)].should.equal_list [stub(:id => 1), stub(:id => 1)]
352
+ assert_assert_success
353
+
354
+ [stub(:id => 1), stub(:id => 2)].should.equal_list [stub(:id => 1), stub(:id => 2)]
355
+ assert_assert_success
356
+
357
+ [stub(:id => 2)].should.not.equal_list [stub(:id => 1)]
358
+ assert_assert_success
359
+
360
+ [stub(:id => 1), stub(:id => 2)].should.not.equal_list [stub(:id => 1)]
361
+ assert_assert_success
362
+
363
+ [stub(:id => 1)].should.not.equal_list [stub(:id => 1), stub(:id => 2)]
364
+ assert_assert_success
365
+
366
+ # equal_list does not ignore order
367
+ [stub(:id => 1), stub(:id => 2)].should.not.equal_list [stub(:id => 2), stub(:id => 1)]
368
+ assert_assert_success
369
+ end
370
+
371
+ it "should fail when equal_list assertions are not correct" do
372
+ [].should.not.equal_list []
373
+ assert_assert_failure("[] has the same records as []")
374
+
375
+ [stub(:id => 1), stub(:id => 1)].should.equal_list [stub(:id => 1)]
376
+ assert_assert_failure("[Mocha::Mock[1], Mocha::Mock[1]] does not have the same records as [Mocha::Mock[1]]")
377
+
378
+ [stub(:id => 1), stub(:id => 2)].should.equal_list [stub(:id => 1), stub(:id => 1), stub(:id => 2)]
379
+ assert_assert_failure("[Mocha::Mock[1], Mocha::Mock[2]] does not have the same records as [Mocha::Mock[1], Mocha::Mock[1], Mocha::Mock[2]]")
380
+
381
+ [stub(:id => 1), stub(:id => 2), stub(:id => 1)].should.equal_list [stub(:id => 1), stub(:id => 2), stub(:id => 2)]
382
+ assert_assert_failure("[Mocha::Mock[1], Mocha::Mock[2], Mocha::Mock[1]] does not have the same records as [Mocha::Mock[1], Mocha::Mock[2], Mocha::Mock[2]]")
383
+
384
+ [stub(:id => 1)].should.not.equal_list [stub(:id => 1)]
385
+ assert_assert_failure("[Mocha::Mock[1]] has the same records as [Mocha::Mock[1]]")
386
+
387
+ [stub(:id => 1), stub(:id => 2)].should.not.equal_list [stub(:id => 1), stub(:id => 2)]
388
+ assert_assert_failure("[Mocha::Mock[1], Mocha::Mock[2]] has the same records as [Mocha::Mock[1], Mocha::Mock[2]]")
389
+
390
+ [stub(:id => 2)].should.equal_list [stub(:id => 1)]
391
+ assert_assert_failure("[Mocha::Mock[2]] does not have the same records as [Mocha::Mock[1]]")
392
+
393
+ [stub(:id => 1), stub(:id => 2)].should.equal_list [stub(:id => 1)]
394
+ assert_assert_failure("[Mocha::Mock[1], Mocha::Mock[2]] does not have the same records as [Mocha::Mock[1]]")
395
+
396
+ [stub(:id => 1)].should.equal_list [stub(:id => 1), stub(:id => 2)]
397
+ assert_assert_failure("[Mocha::Mock[1]] does not have the same records as [Mocha::Mock[1], Mocha::Mock[2]]")
398
+
399
+ # equal_list does not ignore order
400
+ [stub(:id => 1), stub(:id => 2)].should.equal_list [stub(:id => 2), stub(:id => 1)]
401
+ assert_assert_failure("[Mocha::Mock[1], Mocha::Mock[2]] does not have the same records as [Mocha::Mock[2], Mocha::Mock[1]]")
402
+ end
403
+ end
404
+
405
+ class TestController
406
+ def request
407
+ unless @request
408
+ @request ||= Object.new
409
+ def @request.env
410
+ @env ||= {}
411
+ end
412
+ end
413
+ @request
414
+ end
415
+ end
416
+
417
+ describe "Redirection expectations" do
418
+ include AssertionAssertions
419
+
420
+ attr_reader :controller
421
+
422
+ before do
423
+ TestingAssertionsThemselves.assertions = []
424
+
425
+ @controller = TestController.new
426
+ @controller.stubs(:url_for).returns(@url)
427
+
428
+ @matcher = stub('matcher')
429
+ self.stubs(:should).returns(@matcher)
430
+ @matcher.stubs(:redirect_to)
431
+
432
+ @url = 'new_session_url'
433
+ end
434
+
435
+ it "should set the request env HTTP_REFERER before executing the proc" do
436
+ @controller.expects(:url_for).with(@url).returns(@url)
437
+ lambda {}.should.redirect_back_to(@url)
438
+ assert_equal @url, controller.request.env['HTTP_REFERER']
439
+ end
440
+
441
+ it "should call the `redirect_to' matcher with the url _after_ executing the proc" do
442
+ def @matcher.redirect_to(*args)
443
+ raise "Oh noes, should not be called yet!"
444
+ end
445
+
446
+ lambda {
447
+ def @matcher.redirect_to(*args)
448
+ @called = true
449
+ end
450
+ }.should.redirect_back_to(@url)
451
+
452
+ assert @matcher.instance_variable_get(:@called)
453
+ end
454
+
455
+ it "should work with regular url options as well" do
456
+ @controller.expects(:url_for).with(:action => :new).returns(@url)
457
+ @matcher.expects(:redirect_to).with(@url)
458
+
459
+ lambda {}.should.redirect_back_to(:action => :new)
460
+ assert_equal @url, controller.request.env['HTTP_REFERER']
461
+ end
462
+
463
+ it "should return the result of calling the block" do
464
+ result = lambda { "called block" }.should.redirect_back_to(@url)
465
+ assert_equal "called block", result
466
+ end
467
+ end
@@ -0,0 +1,80 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+ require 'test/spec/rails/macros'
3
+
4
+ describe "TestGenerator, concerning generation" do
5
+ before do
6
+ @test = mock('Test')
7
+ @generator = Test::Spec::Rails::Macros::Authorization::TestGenerator.new(@test, :access_denied?, 'Expected access to be denied')
8
+ end
9
+
10
+ it "should generate a test description for a GET" do
11
+ @test.expects(:it).with("should disallow GET on `index'")
12
+ @generator.get(:index)
13
+ end
14
+
15
+ it "should generate a test description for a POST with params" do
16
+ @test.expects(:it).with("should disallow POST on `create\' {:venue=>{:name=>\"Bitterzoet\"}}")
17
+ @generator.post(:create, :venue => { :name => "Bitterzoet" })
18
+ end
19
+
20
+ it "should raise a NoMethodError when you disallow an unknown HTTP verb" do
21
+ lambda {
22
+ @generator.unknown :index
23
+ }.should.raise(NoMethodError)
24
+ end
25
+ end
26
+
27
+ class Immediate
28
+ def self.it(description, &block)
29
+ block.call
30
+ end
31
+ end
32
+
33
+ describe "TestGenerator, concerning test contents" do
34
+ before do
35
+ @generator = Test::Spec::Rails::Macros::Authorization::TestGenerator.new(Immediate, :access_denied?, 'Expected access to be denied')
36
+ @generator.stubs(:send).with(:access_denied?).returns(true)
37
+ end
38
+
39
+ it "should send the verb and options to the controller" do
40
+ params = {:venue => {:name => "Bitterzoet"}}
41
+ @generator.stubs(:immediate_values).with(params).returns(params)
42
+ @generator.expects(:send).with(:post, :create, params)
43
+
44
+ @generator.post(:create, params)
45
+ end
46
+
47
+ it "should immediate values in params" do
48
+ params = {:name => 'bitterzoet'}
49
+
50
+ @generator.expects(:immediate_values).with(params).returns(params)
51
+ @generator.stubs(:send).returns(true)
52
+
53
+ @generator.post(:create, params)
54
+ end
55
+ end
56
+
57
+ describe "Macros::Authorization" do
58
+ before do
59
+ @test_case = mock('TestCase')
60
+ @proxy = Test::Spec::Rails::Macros::Should.new(@test_case)
61
+ end
62
+
63
+ it "should return a test generator when a new disallow rule is invoked" do
64
+ generator = @proxy.disallow
65
+
66
+ generator.should.is_a(Test::Spec::Rails::Macros::Authorization::TestGenerator)
67
+ generator.test_case.should == @test_case
68
+ generator.validation_method.should == :access_denied?
69
+ generator.message.should == 'Expected access to be denied'
70
+ end
71
+
72
+ it "should return a test generator when a new login_required rule is invoked" do
73
+ generator = @proxy.require_login
74
+
75
+ generator.should.is_a(Test::Spec::Rails::Macros::Authorization::TestGenerator)
76
+ generator.test_case.should == @test_case
77
+ generator.validation_method.should == :login_required?
78
+ generator.message.should == 'Expected login to be required'
79
+ end
80
+ end
@@ -0,0 +1,78 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+ require 'test/spec/rails/macros'
3
+
4
+ describe "Macros::Proxy" do
5
+ it "should store an instance of the test case" do
6
+ test_case = mock('TestCase')
7
+ proxy = Test::Spec::Rails::Macros::Proxy.new(test_case)
8
+ proxy.test_case.should == test_case
9
+ end
10
+ end
11
+
12
+ describe "Macros::Should" do
13
+ it "should store an instance of the test_case" do
14
+ test_case = mock('TestCase')
15
+ proxy = Test::Spec::Rails::Macros::Should.new(test_case)
16
+ proxy.test_case.should == test_case
17
+ end
18
+ end
19
+
20
+ class Case
21
+ extend Test::Spec::Rails::Macros::ClassMethods
22
+ include Test::Spec::Rails::Macros::InstanceMethods
23
+
24
+ def events(name)
25
+ "fixture: #{name.inspect}"
26
+ end
27
+ end
28
+
29
+ describe "A test case with support for macros" do
30
+ it "should return a Should instance" do
31
+ Case.new.should.should.be.kind_of?(Test::Spec::Rails::Macros::Should)
32
+ end
33
+
34
+ it "should know what are valid fixtures" do
35
+ Case.stubs(:fixture_table_names).returns(%w(events venues))
36
+ Case.known_fixture?(:events).should == true
37
+ Case.known_fixture?(:venues).should == true
38
+ Case.known_fixture?(:unknown).should == false
39
+ end
40
+
41
+ it "should know there are no fixtures when there are no fixture_table_names in the test" do
42
+ Case.stubs(:respond_to?).with(:fixture_table_names).returns(false)
43
+ Case.known_fixture?(:events).should == false
44
+ Case.known_fixture?(:venues).should == false
45
+ Case.known_fixture?(:unknown).should == false
46
+ end
47
+
48
+ it "should wrap know fixture methods in a lambda so they don't have to be known yet" do
49
+ Case.stubs(:fixture_table_names).returns(%w(events venues))
50
+ Case.events(:bitterzoet).should.be.kind_of?(Test::Spec::Rails::Macros::LazyValue)
51
+ Case.events(:bitterzoet).value.should == "events(:bitterzoet)"
52
+ end
53
+ end
54
+
55
+ describe "A test case instance with support for macros" do
56
+ before do
57
+ @case = Case.new
58
+ Case.stubs(:fixture_table_names).returns(%w(events))
59
+ end
60
+
61
+ it "should immediate LazyValue values in the context of the current test" do
62
+ @case.immediate_values(:name => Test::Spec::Rails::Macros::LazyValue.new('events(:bitterzoet)')).should == {
63
+ :name => 'fixture: :bitterzoet'
64
+ }
65
+ end
66
+
67
+ it "should immediate Proc values" do
68
+ @case.immediate_values(:name => Proc.new { 42 }).should == { :name => 42 }
69
+ end
70
+
71
+ it "should not change actual values" do
72
+ @case.immediate_values(:name => 42).should == { :name => 42 }
73
+ end
74
+
75
+ it "should immediate nested values" do
76
+ @case.immediate_values(:venue => { :name => Proc.new { 'bitterzoet' } }).should == {:venue => {:name => 'bitterzoet'}}
77
+ end
78
+ end
@@ -0,0 +1,52 @@
1
+ require 'rubygems' rescue LoadError
2
+ require 'test/spec'
3
+
4
+ require File.expand_path('../../lib/test/spec/share', __FILE__)
5
+
6
+ class DummyMock
7
+ extend SharedSpecsInclusionHelper
8
+
9
+ class << self
10
+ attr_reader :times_called
11
+ def it(name, &block)
12
+ @times_called ||= 0
13
+ @times_called += 1
14
+ end
15
+ end
16
+ end
17
+
18
+ share "Dummy" do
19
+ it("spec 1") {}
20
+ it("spec 2") {}
21
+ end
22
+
23
+ describe "Shared specs" do
24
+ it "should define a global variable that will hold all the shared specs" do
25
+ $shared_specs.should.be.instance_of Hash
26
+ end
27
+ end
28
+
29
+ describe "Kernel#share" do
30
+ it "should add the shared specs to the global shared modules variable" do
31
+ before = $shared_specs.length
32
+ share("Bar") {}
33
+ $shared_specs.length.should == before + 1
34
+ end
35
+
36
+ it "should have stored the proc that holds the specs" do
37
+ $shared_specs['Dummy'].should.be.instance_of Proc
38
+ end
39
+ end
40
+
41
+ describe "SharedSpecsInclusionHelper::shared_specs_for" do
42
+ it "should have extended Test::Unit::TestCase" do
43
+ Test::Unit::TestCase.should.respond_to :shared_specs_for
44
+ end
45
+
46
+ it "should return the specified module containing the shared specs" do
47
+ DummyMock.class_eval do
48
+ shared_specs_for 'Dummy'
49
+ end
50
+ DummyMock.times_called.should.be 2
51
+ end
52
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems' rescue LoadError
2
+ require 'test/spec'
3
+ require 'mocha'
4
+
5
+ require 'active_support'
6
+ require 'active_support/test_case'
7
+
8
+ $:.unshift(File.expand_path('../../lib', __FILE__))