Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/activesupport/test/test_test.rb

Revision 8664, 2.7 kB (checked in by bitsweat, 7 months ago)

Extract ActiveSupport::Callbacks from Active Record, test case setup and teardown, and ActionController::Dispatcher. Closes #10727.

Line 
1 require 'abstract_unit'
2 require 'active_support/test_case'
3
4 class AssertDifferenceTest < Test::Unit::TestCase
5   def setup
6     @object = Class.new do
7       attr_accessor :num
8       def increment
9         self.num += 1
10       end
11
12       def decrement
13         self.num -= 1
14       end
15     end.new   
16     @object.num = 0
17   end
18
19   if lambda { }.respond_to?(:binding)
20     def test_assert_no_difference
21       assert_no_difference '@object.num' do
22         # ...
23       end
24     end
25
26     def test_assert_difference
27       assert_difference '@object.num', +1 do
28         @object.increment
29       end
30     end
31
32     def test_assert_difference_with_implicit_difference
33       assert_difference '@object.num' do
34         @object.increment
35       end
36     end
37
38     def test_arbitrary_expression
39       assert_difference '@object.num + 1', +2 do
40         @object.increment
41         @object.increment
42       end
43     end
44
45     def test_negative_differences
46       assert_difference '@object.num', -1 do
47         @object.decrement
48       end
49     end
50
51     def test_expression_is_evaluated_in_the_appropriate_scope
52       local_scope = 'foo'
53       silence_warnings do
54         assert_difference('local_scope; @object.num') { @object.increment }
55       end
56     end
57
58     def test_array_of_expressions
59       assert_difference [ '@object.num', '@object.num + 1' ], +1 do
60         @object.increment
61       end
62     end
63   else
64     def default_test; end
65   end
66 end
67
68 # These should always pass
69 class NotTestingThingsTest < Test::Unit::TestCase
70   include ActiveSupport::Testing::Default
71 end
72
73 class AlsoDoingNothingTest < ActiveSupport::TestCase
74 end
75
76 # Setup and teardown callbacks.
77 class SetupAndTeardownTest < Test::Unit::TestCase
78   setup :reset_callback_record, :foo
79   teardown :foo, :sentinel, :foo
80
81   def test_inherited_setup_callbacks
82     assert_equal [:reset_callback_record, :foo], self.class.setup_callback_chain.map(&:method)
83     assert_equal [:foo], @called_back
84     assert_equal [:foo, :sentinel, :foo], self.class.teardown_callback_chain.map(&:method)
85   end
86
87   protected
88     def reset_callback_record
89       @called_back = []
90     end
91
92     def foo
93       @called_back << :foo
94     end
95
96     def sentinel
97       assert_equal [:foo, :foo], @called_back
98     end
99 end
100
101
102 class SubclassSetupAndTeardownTest < SetupAndTeardownTest
103   setup :bar
104   teardown :bar
105
106   def test_inherited_setup_callbacks
107     assert_equal [:reset_callback_record, :foo, :bar], self.class.setup_callback_chain.map(&:method)
108     assert_equal [:foo, :bar], @called_back
109     assert_equal [:foo, :sentinel, :foo, :bar], self.class.teardown_callback_chain.map(&:method)
110   end
111
112   protected
113     def bar
114       @called_back << :bar
115     end
116
117     def sentinel
118       assert_equal [:foo, :bar, :bar, :foo], @called_back
119     end
120 end
Note: See TracBrowser for help on using the browser.