mocha 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,106 @@
1
+ require 'test_helper'
2
+ require 'smart_test_case/multiple_setup_and_teardown'
3
+ require 'mocha/auto_verify'
4
+ require 'shared/backtracefilter'
5
+ require 'execution_point'
6
+
7
+ class MochaTestResultIntegrationTest < Test::Unit::TestCase
8
+
9
+ def test_should_include_expectation_verification_in_assertion_count
10
+ test_result = run_test do
11
+ object = mock()
12
+ object.expects(:message)
13
+ object.message
14
+ end
15
+ assert_equal 1, test_result.assertion_count
16
+ end
17
+
18
+ def test_should_include_assertions_in_assertion_count
19
+ test_result = run_test do
20
+ assert true
21
+ end
22
+ assert_equal 1, test_result.assertion_count
23
+ end
24
+
25
+ def test_should_not_include_stubbing_expectation_verification_in_assertion_count
26
+ test_result = run_test do
27
+ object = mock()
28
+ object.stubs(:message)
29
+ object.message
30
+ end
31
+ assert_equal 0, test_result.assertion_count
32
+ end
33
+
34
+ def test_should_include_expectation_verification_failure_in_failure_count
35
+ test_result = run_test do
36
+ object = mock()
37
+ object.expects(:message)
38
+ end
39
+ assert_equal 1, test_result.failure_count
40
+ end
41
+
42
+ def test_should_include_unexpected_verification_failure_in_failure_count
43
+ test_result = run_test do
44
+ object = mock()
45
+ object.message
46
+ end
47
+ assert_equal 1, test_result.failure_count
48
+ end
49
+
50
+ def test_should_include_assertion_failure_in_failure_count
51
+ test_result = run_test do
52
+ flunk
53
+ end
54
+ assert_equal 1, test_result.failure_count
55
+ end
56
+
57
+ def test_should_display_backtrace_indicating_line_number_where_expects_was_called
58
+ test_result = Test::Unit::TestResult.new
59
+ faults = []
60
+ test_result.add_listener(Test::Unit::TestResult::FAULT, &lambda { |fault| faults << fault })
61
+ execution_point = nil
62
+ run_test(test_result) do
63
+ object = mock()
64
+ execution_point = ExecutionPoint.current; object.expects(:message)
65
+ end
66
+ assert_equal 1, faults.size
67
+ assert_equal execution_point, ExecutionPoint.new(faults.first.location)
68
+ end
69
+
70
+ def test_should_display_backtrace_indicating_line_number_where_unexpected_method_was_called
71
+ test_result = Test::Unit::TestResult.new
72
+ faults = []
73
+ test_result.add_listener(Test::Unit::TestResult::FAULT, &lambda { |fault| faults << fault })
74
+ execution_point = nil
75
+ run_test(test_result) do
76
+ object = mock()
77
+ execution_point = ExecutionPoint.current; object.message
78
+ end
79
+ assert_equal 1, faults.size
80
+ assert_equal execution_point, ExecutionPoint.new(faults.first.location)
81
+ end
82
+
83
+ def test_should_display_backtrace_indicating_line_number_where_failing_assertion_was_called
84
+ test_result = Test::Unit::TestResult.new
85
+ faults = []
86
+ test_result.add_listener(Test::Unit::TestResult::FAULT, &lambda { |fault| faults << fault })
87
+ execution_point = nil
88
+ run_test(test_result) do
89
+ execution_point = ExecutionPoint.current; flunk
90
+ end
91
+ assert_equal 1, faults.size
92
+ assert_equal execution_point, ExecutionPoint.new(faults.first.location)
93
+ end
94
+
95
+ def run_test(test_result = Test::Unit::TestResult.new, &block)
96
+ test_class = Class.new(Test::Unit::TestCase) do
97
+ include MultipleSetupAndTeardown
98
+ include AutoVerify
99
+ define_method(:test_me, &block)
100
+ end
101
+ test = test_class.new(:test_me)
102
+ test.run(test_result) {}
103
+ test_result
104
+ end
105
+
106
+ end
@@ -4,88 +4,189 @@ require 'active_record_test_case'
4
4
 
5
5
  class MultipleSetupAndTeardownTest < Test::Unit::TestCase
6
6
 
7
- def test_1
7
+ def test_should_call_added_setup
8
8
  test_case = Class.new(Test::Unit::TestCase) do
9
9
  define_method(:methods_called) { @methods_called ||= [] }
10
10
  include MultipleSetupAndTeardown
11
- add_setup_method(:setup_1)
12
- define_method(:setup_1) { methods_called << :setup_1 }
13
- define_method(:test_me) { methods_called << :test_me }
11
+ add_setup_method(:added_setup)
12
+ define_method(:added_setup) { methods_called << :added_setup }
13
+ define_method(:test_method) { methods_called << :test_method }
14
14
  end
15
- test = test_case.new(:test_me)
15
+ test = test_case.new(:test_method)
16
16
  test_result = Test::Unit::TestResult.new
17
17
  test.run(test_result) {}
18
18
  assert test_result.passed?
19
- assert_equal [:setup_1, :test_me], test.methods_called
19
+ assert_equal [:added_setup, :test_method], test.methods_called
20
20
  end
21
21
 
22
- def test_2
22
+ def test_should_call_added_teardown
23
+ test_case = Class.new(Test::Unit::TestCase) do
24
+ define_method(:methods_called) { @methods_called ||= [] }
25
+ include MultipleSetupAndTeardown
26
+ add_teardown_method(:added_teardown)
27
+ define_method(:added_teardown) { methods_called << :added_teardown }
28
+ define_method(:test_method) { methods_called << :test_method }
29
+ end
30
+ test = test_case.new(:test_method)
31
+ test_result = Test::Unit::TestResult.new
32
+ test.run(test_result) {}
33
+ assert test_result.passed?
34
+ assert_equal [:test_method, :added_teardown], test.methods_called
35
+ end
36
+
37
+ def test_should_call_both_added_teardowns_even_if_one_raises_exception
38
+ test_case = Class.new(Test::Unit::TestCase) do
39
+ define_method(:methods_called) { @methods_called ||= [] }
40
+ include MultipleSetupAndTeardown
41
+ add_teardown_method(:added_teardown_1)
42
+ add_teardown_method(:added_teardown_2)
43
+ define_method(:added_teardown_1) { methods_called << :added_teardown_1 }
44
+ define_method(:added_teardown_2) { methods_called << :added_teardown_2; raise }
45
+ define_method(:test_method) { methods_called << :test_method }
46
+ end
47
+ test = test_case.new(:test_method)
48
+ test_result = Test::Unit::TestResult.new
49
+ test.run(test_result) {}
50
+ assert_equal false, test_result.passed?
51
+ assert_equal [:test_method, :added_teardown_2, :added_teardown_1], test.methods_called
52
+ end
53
+
54
+ def test_should_call_added_setup_and_setup_defined_before_module_included
23
55
  test_case = Class.new(Test::Unit::TestCase) do
24
56
  define_method(:methods_called) { @methods_called ||= [] }
25
57
  define_method(:setup) { methods_called << :setup }
26
58
  include MultipleSetupAndTeardown
27
- add_setup_method(:setup_1)
28
- define_method(:setup_1) { methods_called << :setup_1 }
29
- define_method(:test_me) { methods_called << :test_me }
59
+ add_setup_method(:added_setup)
60
+ define_method(:added_setup) { methods_called << :added_setup }
61
+ define_method(:test_method) { methods_called << :test_method }
62
+ end
63
+ test = test_case.new(:test_method)
64
+ test_result = Test::Unit::TestResult.new
65
+ test.run(test_result) {}
66
+ assert test_result.passed?
67
+ assert_equal [:setup, :added_setup, :test_method], test.methods_called
68
+ end
69
+
70
+ def test_should_call_added_teardown_and_teardown_defined_before_module_included
71
+ test_case = Class.new(Test::Unit::TestCase) do
72
+ define_method(:methods_called) { @methods_called ||= [] }
73
+ define_method(:teardown) { methods_called << :teardown }
74
+ include MultipleSetupAndTeardown
75
+ add_teardown_method(:added_teardown)
76
+ define_method(:added_teardown) { methods_called << :added_teardown }
77
+ define_method(:test_method) { methods_called << :test_method }
30
78
  end
31
- test = test_case.new(:test_me)
79
+ test = test_case.new(:test_method)
32
80
  test_result = Test::Unit::TestResult.new
33
81
  test.run(test_result) {}
34
82
  assert test_result.passed?
35
- assert_equal [:setup, :setup_1, :test_me], test.methods_called
83
+ assert_equal [:test_method, :added_teardown, :teardown], test.methods_called
36
84
  end
37
85
 
38
- def test_3
86
+ def test_should_call_added_setup_and_setup_defined_after_module_included
39
87
  test_case = Class.new(Test::Unit::TestCase) do
40
88
  define_method(:methods_called) { @methods_called ||= [] }
41
89
  include MultipleSetupAndTeardown
42
90
  define_method(:setup) { methods_called << :setup }
43
- add_setup_method(:setup_1)
44
- define_method(:setup_1) { methods_called << :setup_1 }
45
- define_method(:test_me) { methods_called << :test_me }
91
+ add_setup_method(:added_setup)
92
+ define_method(:added_setup) { methods_called << :added_setup }
93
+ define_method(:test_method) { methods_called << :test_method }
94
+ end
95
+ test = test_case.new(:test_method)
96
+ test_result = Test::Unit::TestResult.new
97
+ test.run(test_result) {}
98
+ assert test_result.passed?
99
+ assert_equal [:added_setup, :setup, :test_method], test.methods_called
100
+ end
101
+
102
+ def test_should_call_added_teardown_and_teardown_defined_after_module_included
103
+ test_case = Class.new(Test::Unit::TestCase) do
104
+ define_method(:methods_called) { @methods_called ||= [] }
105
+ include MultipleSetupAndTeardown
106
+ define_method(:teardown) { methods_called << :teardown }
107
+ add_teardown_method(:added_teardown)
108
+ define_method(:added_teardown) { methods_called << :added_teardown }
109
+ define_method(:test_method) { methods_called << :test_method }
46
110
  end
47
- test = test_case.new(:test_me)
111
+ test = test_case.new(:test_method)
48
112
  test_result = Test::Unit::TestResult.new
49
113
  test.run(test_result) {}
50
114
  assert test_result.passed?
51
- assert_equal [:setup_1, :setup, :test_me], test.methods_called
115
+ assert_equal [:test_method, :teardown, :added_teardown], test.methods_called
52
116
  end
53
117
 
54
- def test_4
118
+ def test_should_call_added_setup_and_setup_defined_in_derived_test_case
55
119
  test_case = Class.new(Test::Unit::TestCase) do
56
120
  define_method(:methods_called) { @methods_called ||= [] }
57
121
  include MultipleSetupAndTeardown
58
- add_setup_method(:setup_1)
59
- define_method(:setup_1) { methods_called << :setup_1 }
122
+ add_setup_method(:added_setup)
123
+ define_method(:added_setup) { methods_called << :added_setup }
60
124
  end
61
125
  derived_test_case = Class.new(test_case) do
62
126
  define_method(:setup) { methods_called << :setup }
63
- define_method(:test_me) { methods_called << :test_me }
127
+ define_method(:test_method) { methods_called << :test_method }
64
128
  end
65
- test = derived_test_case.new(:test_me)
129
+ test = derived_test_case.new(:test_method)
66
130
  test_result = Test::Unit::TestResult.new
67
131
  test.run(test_result) {}
68
132
  assert test_result.passed?
69
- assert_equal [:setup_1, :setup, :test_me], test.methods_called
133
+ assert_equal [:added_setup, :setup, :test_method], test.methods_called
70
134
  end
71
135
 
72
- def test_5
136
+ def test_should_call_added_teardown_and_teardown_defined_in_derived_test_case
137
+ test_case = Class.new(Test::Unit::TestCase) do
138
+ define_method(:methods_called) { @methods_called ||= [] }
139
+ include MultipleSetupAndTeardown
140
+ add_teardown_method(:added_teardown)
141
+ define_method(:added_teardown) { methods_called << :added_teardown }
142
+ end
143
+ derived_test_case = Class.new(test_case) do
144
+ define_method(:teardown) { methods_called << :teardown }
145
+ define_method(:test_method) { methods_called << :test_method }
146
+ end
147
+ test = derived_test_case.new(:test_method)
148
+ test_result = Test::Unit::TestResult.new
149
+ test.run(test_result) {}
150
+ assert test_result.passed?
151
+ assert_equal [:test_method, :teardown, :added_teardown], test.methods_called
152
+ end
153
+
154
+ def test_should_call_added_setup_and_setup_defined_in_derived_test_case_with_active_record_setup_with_fixtures
73
155
  test_case = Class.new(Test::Unit::TestCase) do
74
156
  define_method(:methods_called) { @methods_called ||= [] }
75
157
  include ActiveRecordTestCase
76
158
  include MultipleSetupAndTeardown
77
- add_setup_method(:setup_1)
78
- define_method(:setup_1) { methods_called << :setup_1 }
159
+ add_setup_method(:added_setup)
160
+ define_method(:added_setup) { methods_called << :added_setup }
79
161
  end
80
162
  derived_test_case = Class.new(test_case) do
81
163
  define_method(:setup) { methods_called << :setup }
82
- define_method(:test_me) { methods_called << :test_me }
164
+ define_method(:test_method) { methods_called << :test_method }
83
165
  end
84
- test = derived_test_case.new(:test_me)
166
+ test = derived_test_case.new(:test_method)
85
167
  test_result = Test::Unit::TestResult.new
86
168
  test.run(test_result) {}
87
169
  assert test_result.passed?
88
- assert_equal [:setup_with_fixtures, :setup_1, :setup, :test_me, :teardown_with_fixtures], test.methods_called
170
+ assert_equal [:setup_with_fixtures, :added_setup, :setup, :test_method, :teardown_with_fixtures], test.methods_called
89
171
  end
90
-
172
+
173
+ def test_should_call_added_teardown_and_teardown_defined_in_derived_test_case_with_active_record_teardown_with_fixtures
174
+ test_case = Class.new(Test::Unit::TestCase) do
175
+ define_method(:methods_called) { @methods_called ||= [] }
176
+ include ActiveRecordTestCase
177
+ include MultipleSetupAndTeardown
178
+ add_teardown_method(:added_teardown)
179
+ define_method(:added_teardown) { methods_called << :added_teardown }
180
+ end
181
+ derived_test_case = Class.new(test_case) do
182
+ define_method(:teardown) { methods_called << :teardown }
183
+ define_method(:test_method) { methods_called << :test_method }
184
+ end
185
+ test = derived_test_case.new(:test_method)
186
+ test_result = Test::Unit::TestResult.new
187
+ test.run(test_result) {}
188
+ assert test_result.passed?
189
+ assert_equal [:setup_with_fixtures, :test_method, :teardown, :added_teardown, :teardown_with_fixtures], test.methods_called
190
+ end
191
+
91
192
  end
@@ -72,7 +72,8 @@ class AnyInstanceMethodTest < Test::Unit::TestCase
72
72
 
73
73
  def test_should_call_remove_new_method
74
74
  klass = Class.new { def method_x; end }
75
- any_instance = Mock.new(:reset_mocha => nil)
75
+ any_instance = Mock.new
76
+ any_instance.stubs(:reset_mocha)
76
77
  klass.define_instance_method(:any_instance) { any_instance }
77
78
  method = AnyInstanceMethod.new(klass, :method_x)
78
79
  method.replace_instance_method(:restore_original_method) { }
@@ -86,7 +87,8 @@ class AnyInstanceMethodTest < Test::Unit::TestCase
86
87
 
87
88
  def test_should_call_restore_original_method
88
89
  klass = Class.new { def method_x; end }
89
- any_instance = Mock.new(:reset_mocha => nil)
90
+ any_instance = Mock.new
91
+ any_instance.stubs(:reset_mocha)
90
92
  klass.define_instance_method(:any_instance) { any_instance }
91
93
  method = AnyInstanceMethod.new(klass, :method_x)
92
94
  method.replace_instance_method(:remove_new_method) { }
@@ -111,4 +113,14 @@ class AnyInstanceMethodTest < Test::Unit::TestCase
111
113
  assert any_instance.mocha_was_reset
112
114
  end
113
115
 
116
+ def test_should_return_any_instance_mocha_for_stubbee
117
+ mocha = Object.new
118
+ any_instance = Object.new
119
+ any_instance.define_instance_method(:mocha) { mocha }
120
+ stubbee = Class.new
121
+ stubbee.define_instance_method(:any_instance) { any_instance }
122
+ method = AnyInstanceMethod.new(stubbee, :method_name)
123
+ assert_equal stubbee.any_instance.mocha, method.mock
124
+ end
125
+
114
126
  end
@@ -2,6 +2,7 @@ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
2
 
3
3
  require 'stubba/central'
4
4
  require 'mocha/mock'
5
+ require 'method_definer'
5
6
 
6
7
  class CentralTest < Test::Unit::TestCase
7
8
 
@@ -49,8 +50,10 @@ class CentralTest < Test::Unit::TestCase
49
50
 
50
51
  def test_should_unstub_all_methods
51
52
  stubba = Central.new
52
- method_1 = Mock.new(:unstub => nil)
53
- method_2 = Mock.new(:unstub => nil)
53
+ method_1 = Mock.new
54
+ method_1.expects(:unstub)
55
+ method_2 = Mock.new
56
+ method_2.expects(:unstub)
54
57
  stubba.stubba_methods = [method_1, method_2]
55
58
 
56
59
  stubba.unstub_all
@@ -59,5 +62,62 @@ class CentralTest < Test::Unit::TestCase
59
62
  method_1.verify
60
63
  method_2.verify
61
64
  end
65
+
66
+ def test_should_collect_mock_from_all_methods
67
+ method_1 = Mock.new
68
+ method_1.stubs(:mock).returns(:mock_1)
69
+
70
+ method_2 = Mock.new
71
+ method_2.stubs(:mock).returns(:mock_2)
72
+
73
+ stubba = Central.new
74
+ stubba.stubba_methods = [method_1, method_2]
75
+
76
+ assert_equal [:mock_1, :mock_2], stubba.unique_mocks
77
+ end
78
+
79
+ def test_should_return_unique_mochas
80
+ method_1 = Mock.new
81
+ method_1.stubs(:mock).returns(:mock_1)
82
+
83
+ method_2 = Mock.new
84
+ method_2.stubs(:mock).returns(:mock_1)
85
+
86
+ stubba = Central.new
87
+ stubba.stubba_methods = [method_1, method_2]
88
+
89
+ assert_equal [:mock_1], stubba.unique_mocks
90
+ end
91
+
92
+ def test_should_call_verify_on_all_unique_mocks
93
+ mock_class = Class.new do
94
+ attr_accessor :verify_called
95
+ def verify
96
+ self.verify_called = true
97
+ end
98
+ end
99
+ mocks = [mock_class.new, mock_class.new]
100
+ stubba = Central.new
101
+ stubba.replace_instance_method(:unique_mocks) { mocks }
102
+
103
+ stubba.verify_all
104
+
105
+ assert mocks.all? { |mock| mock.verify_called }
106
+ end
107
+
108
+ def test_should_call_verify_on_all_unique_mochas
109
+ mock_class = Class.new do
110
+ def verify(&block)
111
+ yield if block_given?
112
+ end
113
+ end
114
+ stubba = Central.new
115
+ stubba.replace_instance_method(:unique_mocks) { [mock_class.new] }
116
+ yielded = false
117
+
118
+ stubba.verify_all { yielded = true }
119
+
120
+ assert yielded
121
+ end
62
122
 
63
123
  end
@@ -33,6 +33,16 @@ class ClassMethodTest < Test::Unit::TestCase
33
33
  assert klass.respond_to?(hidden_method_x)
34
34
  end
35
35
 
36
+ def test_should_respond_to_original_method_name_after_original_method_has_been_hidden
37
+ klass = Class.new { def self.original_method_name; end }
38
+ method = ClassMethod.new(klass, :original_method_name)
39
+ hidden_method_x = method.hidden_method
40
+
41
+ method.hide_original_method
42
+
43
+ assert klass.respond_to?(:original_method_name)
44
+ end
45
+
36
46
  def test_should_not_hide_original_method_if_method_not_defined
37
47
  klass = Class.new
38
48
  method = ClassMethod.new(klass, :method_x)
@@ -75,7 +85,7 @@ class ClassMethodTest < Test::Unit::TestCase
75
85
  method.restore_original_method
76
86
 
77
87
  assert_equal :original_result, klass.method_x
78
- assert !klass.respond_to?(hidden_method_x)
88
+ assert_equal false, klass.respond_to?(hidden_method_x)
79
89
  end
80
90
 
81
91
  def test_should_not_restore_original_method_if_hidden_method_is_not_defined
@@ -144,5 +154,13 @@ class ClassMethodTest < Test::Unit::TestCase
144
154
 
145
155
  assert klass.reset_called
146
156
  end
157
+
158
+ def test_should_return_mock_for_stubbee
159
+ mocha = Object.new
160
+ stubbee = Object.new
161
+ stubbee.define_instance_accessor(:mocha) { mocha }
162
+ method = ClassMethod.new(stubbee, :method_name)
163
+ assert_equal stubbee.mocha, method.mock
164
+ end
147
165
 
148
166
  end