mocha 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +4 -4
- data/RELEASE +32 -0
- data/Rakefile +3 -3
- data/examples/misc.rb +36 -0
- data/examples/mocha.rb +26 -0
- data/examples/stubba.rb +65 -0
- data/lib/mocha.rb +17 -5
- data/lib/{stubba → mocha}/any_instance_method.rb +2 -2
- data/lib/mocha/auto_verify.rb +7 -7
- data/lib/{stubba → mocha}/central.rb +2 -2
- data/lib/{stubba → mocha}/class_method.rb +7 -10
- data/lib/mocha/expectation.rb +88 -33
- data/lib/mocha/expectation_error.rb +6 -0
- data/lib/mocha/inspect.rb +1 -1
- data/lib/mocha/instance_method.rb +8 -0
- data/lib/mocha/metaclass.rb +1 -1
- data/lib/mocha/mock.rb +8 -8
- data/lib/mocha/mock_methods.rb +60 -16
- data/lib/{stubba → mocha}/object.rb +8 -10
- data/lib/mocha/setup_and_teardown.rb +23 -0
- data/lib/mocha/standalone.rb +30 -0
- data/lib/mocha/test_case_adapter.rb +49 -0
- data/lib/mocha_standalone.rb +2 -0
- data/lib/stubba.rb +2 -8
- data/test/all_tests.rb +9 -10
- data/test/method_definer.rb +2 -2
- data/test/{stubba → mocha}/any_instance_method_test.rb +1 -3
- data/test/mocha/auto_verify_test.rb +9 -10
- data/test/{stubba → mocha}/central_test.rb +5 -4
- data/test/{stubba → mocha}/class_method_test.rb +40 -10
- data/test/mocha/expectation_test.rb +144 -67
- data/test/mocha/inspect_test.rb +12 -1
- data/test/mocha/metaclass_test.rb +22 -0
- data/test/mocha/mock_methods_test.rb +65 -7
- data/test/mocha/mock_test.rb +41 -4
- data/test/{stubba → mocha}/object_test.rb +9 -9
- data/test/mocha/setup_and_teardown_test.rb +76 -0
- data/test/mocha_acceptance_test.rb +8 -8
- data/test/mocha_test_result_integration_test.rb +5 -6
- data/test/standalone_acceptance_test.rb +110 -0
- data/test/stubba_acceptance_test.rb +2 -2
- data/test/stubba_integration_test.rb +17 -24
- data/test/stubba_test_result_integration_test.rb +5 -6
- metadata +22 -18
- data/lib/shared/backtracefilter.rb +0 -46
- data/lib/smart_test_case.rb +0 -5
- data/lib/smart_test_case/multiple_setup_and_teardown.rb +0 -123
- data/lib/stubba/instance_method.rb +0 -18
- data/lib/stubba/setup_and_teardown.rb +0 -25
- data/test/smart_test_case/multiple_setup_and_teardown_test.rb +0 -192
- data/test/stubba/instance_method_test.rb +0 -46
- data/test/stubba/setup_and_teardown_test.rb +0 -134
@@ -1,13 +1,12 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'mocha/central'
|
4
4
|
require 'mocha/mock'
|
5
5
|
require 'method_definer'
|
6
6
|
|
7
7
|
class CentralTest < Test::Unit::TestCase
|
8
8
|
|
9
9
|
include Mocha
|
10
|
-
include Stubba
|
11
10
|
|
12
11
|
def test_should_start_with_empty_stubba_methods
|
13
12
|
stubba = Central.new
|
@@ -63,7 +62,7 @@ class CentralTest < Test::Unit::TestCase
|
|
63
62
|
method_2.verify
|
64
63
|
end
|
65
64
|
|
66
|
-
def
|
65
|
+
def test_should_collect_mocks_from_all_methods
|
67
66
|
method_1 = Mock.new
|
68
67
|
method_1.stubs(:mock).returns(:mock_1)
|
69
68
|
|
@@ -73,7 +72,9 @@ class CentralTest < Test::Unit::TestCase
|
|
73
72
|
stubba = Central.new
|
74
73
|
stubba.stubba_methods = [method_1, method_2]
|
75
74
|
|
76
|
-
assert_equal
|
75
|
+
assert_equal 2, stubba.unique_mocks.size
|
76
|
+
assert stubba.unique_mocks.include?(:mock_1)
|
77
|
+
assert stubba.unique_mocks.include?(:mock_2)
|
77
78
|
end
|
78
79
|
|
79
80
|
def test_should_return_unique_mochas
|
@@ -2,25 +2,55 @@ require File.join(File.dirname(__FILE__), "..", "test_helper")
|
|
2
2
|
require 'method_definer'
|
3
3
|
require 'mocha/mock'
|
4
4
|
|
5
|
-
require '
|
5
|
+
require 'mocha/class_method'
|
6
6
|
|
7
7
|
class ClassMethodTest < Test::Unit::TestCase
|
8
8
|
|
9
|
-
include
|
9
|
+
include Mocha
|
10
10
|
|
11
|
-
def
|
11
|
+
def test_should_provide_hidden_version_of_method_name_starting_with_prefix
|
12
12
|
method = ClassMethod.new(nil, :original_method_name)
|
13
|
-
|
13
|
+
assert_match /^__stubba__/, method.hidden_method
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
17
|
-
method = ClassMethod.new(nil, :original_method_name
|
18
|
-
|
16
|
+
def test_should_provide_hidden_version_of_method_name_ending_with_suffix
|
17
|
+
method = ClassMethod.new(nil, :original_method_name)
|
18
|
+
assert_match /__stubba__$/, method.hidden_method
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_should_provide_hidden_version_of_method_name_including_original_method_name
|
22
|
+
method = ClassMethod.new(nil, :original_method_name)
|
23
|
+
assert_match /original_method_name/, method.hidden_method
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_should_provide_hidden_version_of_method_name_substituting_question_mark
|
27
|
+
method = ClassMethod.new(nil, :question_mark?)
|
28
|
+
assert_no_match /\?/, method.hidden_method
|
29
|
+
assert_match /question_mark_substituted_character_63/, method.hidden_method
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_should_provide_hidden_version_of_method_name_substituting_exclamation_mark
|
33
|
+
method = ClassMethod.new(nil, :exclamation_mark!)
|
34
|
+
assert_no_match /!/, method.hidden_method
|
35
|
+
assert_match /exclamation_mark_substituted_character_33/, method.hidden_method
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_should_provide_hidden_version_of_method_name_substituting_equals_sign
|
39
|
+
method = ClassMethod.new(nil, :equals_sign=)
|
40
|
+
assert_no_match /\=/, method.hidden_method
|
41
|
+
assert_match /equals_sign_substituted_character_61/, method.hidden_method
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_should_provide_hidden_version_of_method_name_substituting_brackets
|
45
|
+
method = ClassMethod.new(nil, :[])
|
46
|
+
assert_no_match /\[\]/, method.hidden_method
|
47
|
+
assert_match /substituted_character_91__substituted_character_93/, method.hidden_method
|
19
48
|
end
|
20
49
|
|
21
|
-
def
|
22
|
-
method = ClassMethod.new(nil,
|
23
|
-
|
50
|
+
def test_should_provide_hidden_version_of_method_name_substituting_plus_sign
|
51
|
+
method = ClassMethod.new(nil, :+)
|
52
|
+
assert_no_match /\+/, method.hidden_method
|
53
|
+
assert_match /substituted_character_43/, method.hidden_method
|
24
54
|
end
|
25
55
|
|
26
56
|
def test_should_hide_original_method
|
@@ -7,76 +7,77 @@ class ExpectationTest < Test::Unit::TestCase
|
|
7
7
|
|
8
8
|
include Mocha
|
9
9
|
|
10
|
+
def new_expectation
|
11
|
+
Expectation.new(nil, :expected_method)
|
12
|
+
end
|
13
|
+
|
10
14
|
def test_should_match_calls_to_same_method_with_any_parameters
|
11
|
-
|
12
|
-
assert expectation.match?(:expected_method, 1, 2, 3)
|
15
|
+
assert new_expectation.match?(:expected_method, 1, 2, 3)
|
13
16
|
end
|
14
17
|
|
15
18
|
def test_should_match_calls_to_same_method_with_exactly_zero_parameters
|
16
|
-
expectation =
|
19
|
+
expectation = new_expectation.with()
|
17
20
|
assert expectation.match?(:expected_method)
|
18
21
|
end
|
19
22
|
|
20
23
|
def test_should_not_match_calls_to_same_method_with_more_than_zero_parameters
|
21
|
-
expectation =
|
24
|
+
expectation = new_expectation.with()
|
22
25
|
assert !expectation.match?(:expected_method, 1, 2, 3)
|
23
26
|
end
|
24
27
|
|
25
28
|
def test_should_match_calls_to_same_method_with_expected_parameter_values
|
26
|
-
expectation =
|
29
|
+
expectation = new_expectation.with(1, 2, 3)
|
27
30
|
assert expectation.match?(:expected_method, 1, 2, 3)
|
28
31
|
end
|
29
32
|
|
30
33
|
def test_should_match_calls_to_same_method_with_parameters_constrained_as_expected
|
31
|
-
expectation =
|
34
|
+
expectation = new_expectation.with() {|x, y, z| x + y == z}
|
32
35
|
assert expectation.match?(:expected_method, 1, 2, 3)
|
33
36
|
end
|
34
37
|
|
35
38
|
def test_should_not_match_calls_to_different_methods_with_no_parameters
|
36
|
-
|
37
|
-
assert !expectation.match?(:unexpected_method)
|
39
|
+
assert !new_expectation.match?(:unexpected_method)
|
38
40
|
end
|
39
41
|
|
40
42
|
def test_should_not_match_calls_to_same_method_with_too_few_parameters
|
41
|
-
expectation =
|
43
|
+
expectation = new_expectation.with(1, 2, 3)
|
42
44
|
assert !expectation.match?(:unexpected_method, 1, 2)
|
43
45
|
end
|
44
46
|
|
45
47
|
def test_should_not_match_calls_to_same_method_with_too_many_parameters
|
46
|
-
expectation =
|
48
|
+
expectation = new_expectation.with(1, 2)
|
47
49
|
assert !expectation.match?(:unexpected_method, 1, 2, 3)
|
48
50
|
end
|
49
51
|
|
50
52
|
def test_should_not_match_calls_to_same_method_with_unexpected_parameter_values
|
51
|
-
expectation =
|
53
|
+
expectation = new_expectation.with(1, 2, 3)
|
52
54
|
assert !expectation.match?(:unexpected_method, 1, 0, 3)
|
53
55
|
end
|
54
56
|
|
55
57
|
def test_should_not_match_calls_to_same_method_with_parameters_not_constrained_as_expected
|
56
|
-
expectation =
|
58
|
+
expectation = new_expectation.with() {|x, y, z| x + y == z}
|
57
59
|
assert !expectation.match?(:expected_method, 1, 0, 3)
|
58
60
|
end
|
59
61
|
|
60
62
|
def test_should_store_provided_backtrace
|
61
63
|
backtrace = Object.new
|
62
|
-
expectation = Expectation.new(:expected_method, backtrace)
|
64
|
+
expectation = Expectation.new(nil, :expected_method, backtrace)
|
63
65
|
assert_equal backtrace, expectation.backtrace
|
64
66
|
end
|
65
67
|
|
66
68
|
def test_should_default_backtrace_to_caller
|
67
|
-
execution_point = ExecutionPoint.current; expectation = Expectation.new(:expected_method)
|
69
|
+
execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
|
68
70
|
assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
|
69
71
|
end
|
70
72
|
|
71
73
|
def test_should_not_yield
|
72
|
-
expectation = Expectation.new(:expected_method)
|
73
74
|
yielded = false
|
74
|
-
|
75
|
+
new_expectation.invoke() { yielded = true }
|
75
76
|
assert_equal false, yielded
|
76
77
|
end
|
77
78
|
|
78
79
|
def test_should_yield_no_parameters
|
79
|
-
expectation =
|
80
|
+
expectation = new_expectation.yields
|
80
81
|
yielded_parameters = nil
|
81
82
|
expectation.invoke() { |*parameters| yielded_parameters = parameters }
|
82
83
|
assert_equal Array.new, yielded_parameters
|
@@ -84,85 +85,130 @@ class ExpectationTest < Test::Unit::TestCase
|
|
84
85
|
|
85
86
|
def test_should_yield_with_specified_parameters
|
86
87
|
parameters_for_yield = [1, 2, 3]
|
87
|
-
expectation =
|
88
|
+
expectation = new_expectation.yields(*parameters_for_yield)
|
88
89
|
yielded_parameters = nil
|
89
90
|
expectation.invoke() { |*parameters| yielded_parameters = parameters }
|
90
91
|
assert_equal parameters_for_yield, yielded_parameters
|
91
92
|
end
|
92
93
|
|
93
94
|
def test_should_return_specified_value
|
94
|
-
expectation =
|
95
|
+
expectation = new_expectation.returns(99)
|
96
|
+
assert_equal 99, expectation.invoke
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_should_return_same_specified_value_multiple_times
|
100
|
+
expectation = new_expectation.returns(99)
|
101
|
+
assert_equal 99, expectation.invoke
|
102
|
+
assert_equal 99, expectation.invoke
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_should_return_specified_values_on_consecutive_calls
|
106
|
+
expectation = new_expectation.returns(99, 100, 101)
|
107
|
+
assert_equal 99, expectation.invoke
|
108
|
+
assert_equal 100, expectation.invoke
|
109
|
+
assert_equal 101, expectation.invoke
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_should_return_specified_values_on_consecutive_calls_even_if_values_are_modified
|
113
|
+
values = [99, 100, 101]
|
114
|
+
expectation = new_expectation.returns(*values)
|
115
|
+
values.shift
|
95
116
|
assert_equal 99, expectation.invoke
|
117
|
+
assert_equal 100, expectation.invoke
|
118
|
+
assert_equal 101, expectation.invoke
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_should_return_nil_by_default
|
122
|
+
assert_nil new_expectation.invoke
|
96
123
|
end
|
97
124
|
|
98
125
|
def test_should_return_nil_if_no_value_specified
|
99
|
-
expectation =
|
126
|
+
expectation = new_expectation.returns()
|
100
127
|
assert_nil expectation.invoke
|
101
128
|
end
|
102
129
|
|
103
130
|
def test_should_return_evaluated_proc
|
104
|
-
|
131
|
+
proc = lambda { 99 }
|
132
|
+
expectation = new_expectation.returns(proc)
|
105
133
|
assert_equal 99, expectation.invoke
|
106
134
|
end
|
107
135
|
|
136
|
+
def test_should_return_evaluated_proc_without_using_is_a_method
|
137
|
+
proc = lambda { 99 }
|
138
|
+
proc.define_instance_accessor(:called)
|
139
|
+
proc.called = false
|
140
|
+
proc.replace_instance_method(:is_a?) { self.called = true; true}
|
141
|
+
expectation = new_expectation.returns(proc)
|
142
|
+
expectation.invoke
|
143
|
+
assert_equal false, proc.called
|
144
|
+
end
|
145
|
+
|
108
146
|
def test_should_raise_runtime_exception
|
109
|
-
expectation =
|
147
|
+
expectation = new_expectation.raises
|
110
148
|
assert_raise(RuntimeError) { expectation.invoke }
|
111
149
|
end
|
112
150
|
|
113
151
|
def test_should_raise_custom_exception
|
114
152
|
exception = Class.new(Exception)
|
115
|
-
expectation =
|
153
|
+
expectation = new_expectation.raises(exception)
|
116
154
|
assert_raise(exception) { expectation.invoke }
|
117
155
|
end
|
118
156
|
|
157
|
+
def test_should_raise_same_instance_of_custom_exception
|
158
|
+
exception_klass = Class.new(StandardError)
|
159
|
+
expected_exception = exception_klass.new
|
160
|
+
expectation = new_expectation.raises(expected_exception)
|
161
|
+
actual_exception = assert_raise(exception_klass) { expectation.invoke }
|
162
|
+
assert_same expected_exception, actual_exception
|
163
|
+
end
|
164
|
+
|
119
165
|
def test_should_use_the_default_exception_message
|
120
|
-
expectation =
|
166
|
+
expectation = new_expectation.raises(Exception)
|
121
167
|
exception = assert_raise(Exception) { expectation.invoke }
|
122
168
|
assert_equal Exception.new.message, exception.message
|
123
169
|
end
|
124
170
|
|
125
171
|
def test_should_raise_custom_exception_with_message
|
126
172
|
exception_msg = "exception message"
|
127
|
-
expectation =
|
173
|
+
expectation = new_expectation.raises(Exception, exception_msg)
|
128
174
|
exception = assert_raise(Exception) { expectation.invoke }
|
129
175
|
assert_equal exception_msg, exception.message
|
130
176
|
end
|
131
177
|
|
132
178
|
def test_should_not_raise_error_on_verify_if_expected_call_was_made
|
133
|
-
expectation =
|
179
|
+
expectation = new_expectation
|
134
180
|
expectation.invoke
|
135
|
-
assert_nothing_raised(
|
181
|
+
assert_nothing_raised(ExpectationError) {
|
136
182
|
expectation.verify
|
137
183
|
}
|
138
184
|
end
|
139
185
|
|
140
186
|
def test_should_not_raise_error_on_verify_if_expected_call_was_made_at_least_once
|
141
|
-
expectation =
|
187
|
+
expectation = new_expectation.at_least_once
|
142
188
|
3.times {expectation.invoke}
|
143
|
-
assert_nothing_raised(
|
189
|
+
assert_nothing_raised(ExpectationError) {
|
144
190
|
expectation.verify
|
145
191
|
}
|
146
192
|
end
|
147
193
|
|
148
194
|
def test_should_raise_error_on_verify_if_expected_call_was_not_made_at_least_once
|
149
|
-
expectation =
|
150
|
-
e = assert_raise(
|
195
|
+
expectation = new_expectation.with(1, 2, 3).at_least_once
|
196
|
+
e = assert_raise(ExpectationError) {
|
151
197
|
expectation.verify
|
152
198
|
}
|
153
199
|
assert_match(/expected calls: at least 1, actual calls: 0/i, e.message)
|
154
200
|
end
|
155
201
|
|
156
202
|
def test_should_not_raise_error_on_verify_if_expected_call_was_made_expected_number_of_times
|
157
|
-
expectation =
|
203
|
+
expectation = new_expectation.times(2)
|
158
204
|
2.times {expectation.invoke}
|
159
|
-
assert_nothing_raised(
|
205
|
+
assert_nothing_raised(ExpectationError) {
|
160
206
|
expectation.verify
|
161
207
|
}
|
162
208
|
end
|
163
209
|
|
164
210
|
def test_should_expect_call_not_to_be_made
|
165
|
-
expectation =
|
211
|
+
expectation = new_expectation
|
166
212
|
expectation.define_instance_accessor(:how_many_times)
|
167
213
|
expectation.replace_instance_method(:times) { |how_many_times| self.how_many_times = how_many_times }
|
168
214
|
expectation.never
|
@@ -170,24 +216,24 @@ class ExpectationTest < Test::Unit::TestCase
|
|
170
216
|
end
|
171
217
|
|
172
218
|
def test_should_raise_error_on_verify_if_expected_call_was_made_too_few_times
|
173
|
-
expectation =
|
219
|
+
expectation = new_expectation.times(2)
|
174
220
|
1.times {expectation.invoke}
|
175
|
-
e = assert_raise(
|
221
|
+
e = assert_raise(ExpectationError) {
|
176
222
|
expectation.verify
|
177
223
|
}
|
178
224
|
assert_match(/expected calls: 2, actual calls: 1/i, e.message)
|
179
225
|
end
|
180
226
|
|
181
227
|
def test_should_raise_error_on_verify_if_expected_call_was_made_too_many_times
|
182
|
-
expectation =
|
228
|
+
expectation = new_expectation.times(2)
|
183
229
|
3.times {expectation.invoke}
|
184
|
-
assert_raise(
|
230
|
+
assert_raise(ExpectationError) {
|
185
231
|
expectation.verify
|
186
232
|
}
|
187
233
|
end
|
188
234
|
|
189
235
|
def test_should_yield_self_to_block
|
190
|
-
expectation =
|
236
|
+
expectation = new_expectation
|
191
237
|
expectation.invoke
|
192
238
|
yielded_expectation = nil
|
193
239
|
expectation.verify { |x| yielded_expectation = x }
|
@@ -195,22 +241,21 @@ class ExpectationTest < Test::Unit::TestCase
|
|
195
241
|
end
|
196
242
|
|
197
243
|
def test_should_yield_to_block_before_raising_exception
|
198
|
-
expectation = Expectation.new(:expected_method)
|
199
244
|
yielded = false
|
200
|
-
assert_raise(
|
201
|
-
|
245
|
+
assert_raise(ExpectationError) {
|
246
|
+
new_expectation.verify { |x| yielded = true }
|
202
247
|
}
|
203
248
|
assert yielded
|
204
249
|
end
|
205
250
|
|
206
251
|
def test_should_store_backtrace_from_point_where_expectation_was_created
|
207
|
-
execution_point = ExecutionPoint.current; expectation = Expectation.new(:expected_method)
|
252
|
+
execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
|
208
253
|
assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
|
209
254
|
end
|
210
255
|
|
211
256
|
def test_should_set_backtrace_on_assertion_failed_error_to_point_where_expectation_was_created
|
212
|
-
execution_point = ExecutionPoint.current; expectation = Expectation.new(:expected_method)
|
213
|
-
error = assert_raise(
|
257
|
+
execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
|
258
|
+
error = assert_raise(ExpectationError) {
|
214
259
|
expectation.verify
|
215
260
|
}
|
216
261
|
assert_equal execution_point, ExecutionPoint.new(error.backtrace)
|
@@ -218,63 +263,95 @@ class ExpectationTest < Test::Unit::TestCase
|
|
218
263
|
|
219
264
|
def test_should_display_expectation_message_in_exception_message
|
220
265
|
options = [:a, :b, {:c => 1, :d => 2}]
|
221
|
-
expectation =
|
222
|
-
exception = assert_raise(
|
266
|
+
expectation = new_expectation.with(*options)
|
267
|
+
exception = assert_raise(ExpectationError) {
|
223
268
|
expectation.verify
|
224
269
|
}
|
225
|
-
assert exception.message.include?(expectation.
|
270
|
+
assert exception.message.include?(expectation.method_signature)
|
226
271
|
end
|
227
272
|
|
228
273
|
def test_should_combine_method_name_and_pretty_parameters
|
229
274
|
arguments = 1, 2, {'a' => true, :b => false}, [1, 2, 3]
|
230
|
-
expectation =
|
231
|
-
assert_equal "
|
275
|
+
expectation = new_expectation.with(*arguments)
|
276
|
+
assert_equal "expected_method(#{PrettyParameters.new(arguments).pretty})", expectation.method_signature
|
232
277
|
end
|
233
278
|
|
234
279
|
def test_should_not_include_parameters_in_message
|
235
|
-
|
236
|
-
assert_equal ":meth('** any **')", expectation.message
|
280
|
+
assert_equal "expected_method", new_expectation.method_signature
|
237
281
|
end
|
238
282
|
|
239
283
|
def test_should_always_verify_successfully
|
240
|
-
stub = Stub.new(:
|
284
|
+
stub = Stub.new(nil, :expected_method)
|
241
285
|
assert stub.verify
|
242
286
|
stub.invoke
|
243
287
|
assert stub.verify
|
244
288
|
end
|
245
289
|
|
290
|
+
def test_should_raise_error_with_message_indicating_which_method_was_expected_to_be_called_on_which_mock_object
|
291
|
+
mock = Class.new { def mocha_inspect; 'mock'; end }.new
|
292
|
+
expectation = Expectation.new(mock, :expected_method)
|
293
|
+
e = assert_raise(ExpectationError) { expectation.verify }
|
294
|
+
assert_match "mock.expected_method", e.message
|
295
|
+
end
|
296
|
+
|
246
297
|
end
|
247
298
|
|
248
299
|
class ExpectationSimilarExpectationsTest < Test::Unit::TestCase
|
249
300
|
|
250
301
|
include Mocha
|
251
|
-
|
252
|
-
attr_reader :expectation
|
253
|
-
def setup
|
254
|
-
@expectation = Expectation.new(:meth).with(2)
|
255
|
-
@mock = Object.new
|
256
|
-
end
|
257
302
|
|
303
|
+
def new_expectation
|
304
|
+
Expectation.new(nil, :expected_method)
|
305
|
+
end
|
306
|
+
|
258
307
|
def test_should_find_expectations_to_the_same_method
|
259
|
-
|
308
|
+
expectation = new_expectation.with(1)
|
309
|
+
mock = Object.new
|
310
|
+
mock.define_instance_method(:expectations) { [expectation] }
|
311
|
+
failed_expectation = MissingExpectation.new(mock, :expected_method).with(2)
|
260
312
|
assert_equal [expectation], failed_expectation.similar_expectations
|
261
313
|
end
|
262
314
|
|
263
315
|
def test_should_report_similar_expectations
|
264
|
-
|
265
|
-
|
266
|
-
|
316
|
+
mock = Object.new
|
317
|
+
expectation_1 = new_expectation.with(1)
|
318
|
+
expectation_2 = new_expectation.with(2)
|
319
|
+
mock = Object.new
|
320
|
+
mock.define_instance_method(:expectations) { [expectation_1, expectation_2] }
|
321
|
+
missing_expectation = MissingExpectation.new(mock, :expected_method).with(3)
|
322
|
+
exception = assert_raise(ExpectationError) { missing_expectation.verify }
|
323
|
+
assert_equal "#{mock.mocha_inspect}.expected_method(3) - expected calls: 0, actual calls: 1\nSimilar expectations:\nexpected_method(1)\nexpected_method(2)", exception.message
|
267
324
|
end
|
268
325
|
|
269
326
|
def test_should_ignore_expectations_to_different_methods
|
270
|
-
|
327
|
+
expectation = new_expectation.with(1)
|
328
|
+
mock = Object.new
|
329
|
+
mock.define_instance_method(:expectations) { [expectation] }
|
330
|
+
failed_expectation = MissingExpectation.new(mock, :other_method).with(1)
|
271
331
|
assert failed_expectation.similar_expectations.empty?
|
272
332
|
end
|
273
333
|
|
274
334
|
def test_should_not_report_similar_expectations
|
275
|
-
|
276
|
-
|
277
|
-
|
335
|
+
expectation = new_expectation.with(1)
|
336
|
+
mock = Object.new
|
337
|
+
mock.define_instance_method(:expectations) { [expectation] }
|
338
|
+
mock.define_instance_method(:mocha_inspect) { 'mocha_inspect' }
|
339
|
+
missing_expectation = MissingExpectation.new(mock, :unexpected_method).with(1)
|
340
|
+
exception = assert_raise(ExpectationError) { missing_expectation.verify }
|
341
|
+
assert_equal "mocha_inspect.unexpected_method(1) - expected calls: 0, actual calls: 1", exception.message
|
342
|
+
end
|
343
|
+
|
344
|
+
def test_should_exclude_mocha_locations_from_backtrace
|
345
|
+
mocha_lib = "/username/workspace/mocha_wibble/lib/"
|
346
|
+
backtrace = [ mocha_lib + 'exclude/me/1', mocha_lib + 'exclude/me/2', '/keep/me', mocha_lib + 'exclude/me/3']
|
347
|
+
expectation = Expectation.new(nil, :expected_method, backtrace)
|
348
|
+
expectation.define_instance_method(:mocha_lib_directory) { mocha_lib }
|
349
|
+
assert_equal ['/keep/me'], expectation.filtered_backtrace
|
350
|
+
end
|
351
|
+
|
352
|
+
def test_should_determine_path_for_mocha_lib_directory
|
353
|
+
expectation = new_expectation()
|
354
|
+
assert_match Regexp.new("/lib/$"), expectation.mocha_lib_directory
|
278
355
|
end
|
279
356
|
|
280
357
|
end
|