mocha 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/README +4 -4
  2. data/RELEASE +32 -0
  3. data/Rakefile +3 -3
  4. data/examples/misc.rb +36 -0
  5. data/examples/mocha.rb +26 -0
  6. data/examples/stubba.rb +65 -0
  7. data/lib/mocha.rb +17 -5
  8. data/lib/{stubba → mocha}/any_instance_method.rb +2 -2
  9. data/lib/mocha/auto_verify.rb +7 -7
  10. data/lib/{stubba → mocha}/central.rb +2 -2
  11. data/lib/{stubba → mocha}/class_method.rb +7 -10
  12. data/lib/mocha/expectation.rb +88 -33
  13. data/lib/mocha/expectation_error.rb +6 -0
  14. data/lib/mocha/inspect.rb +1 -1
  15. data/lib/mocha/instance_method.rb +8 -0
  16. data/lib/mocha/metaclass.rb +1 -1
  17. data/lib/mocha/mock.rb +8 -8
  18. data/lib/mocha/mock_methods.rb +60 -16
  19. data/lib/{stubba → mocha}/object.rb +8 -10
  20. data/lib/mocha/setup_and_teardown.rb +23 -0
  21. data/lib/mocha/standalone.rb +30 -0
  22. data/lib/mocha/test_case_adapter.rb +49 -0
  23. data/lib/mocha_standalone.rb +2 -0
  24. data/lib/stubba.rb +2 -8
  25. data/test/all_tests.rb +9 -10
  26. data/test/method_definer.rb +2 -2
  27. data/test/{stubba → mocha}/any_instance_method_test.rb +1 -3
  28. data/test/mocha/auto_verify_test.rb +9 -10
  29. data/test/{stubba → mocha}/central_test.rb +5 -4
  30. data/test/{stubba → mocha}/class_method_test.rb +40 -10
  31. data/test/mocha/expectation_test.rb +144 -67
  32. data/test/mocha/inspect_test.rb +12 -1
  33. data/test/mocha/metaclass_test.rb +22 -0
  34. data/test/mocha/mock_methods_test.rb +65 -7
  35. data/test/mocha/mock_test.rb +41 -4
  36. data/test/{stubba → mocha}/object_test.rb +9 -9
  37. data/test/mocha/setup_and_teardown_test.rb +76 -0
  38. data/test/mocha_acceptance_test.rb +8 -8
  39. data/test/mocha_test_result_integration_test.rb +5 -6
  40. data/test/standalone_acceptance_test.rb +110 -0
  41. data/test/stubba_acceptance_test.rb +2 -2
  42. data/test/stubba_integration_test.rb +17 -24
  43. data/test/stubba_test_result_integration_test.rb +5 -6
  44. metadata +22 -18
  45. data/lib/shared/backtracefilter.rb +0 -46
  46. data/lib/smart_test_case.rb +0 -5
  47. data/lib/smart_test_case/multiple_setup_and_teardown.rb +0 -123
  48. data/lib/stubba/instance_method.rb +0 -18
  49. data/lib/stubba/setup_and_teardown.rb +0 -25
  50. data/test/smart_test_case/multiple_setup_and_teardown_test.rb +0 -192
  51. data/test/stubba/instance_method_test.rb +0 -46
  52. 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 'stubba/central'
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 test_should_collect_mock_from_all_methods
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 [:mock_1, :mock_2], stubba.unique_mocks
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 'stubba/class_method'
5
+ require 'mocha/class_method'
6
6
 
7
7
  class ClassMethodTest < Test::Unit::TestCase
8
8
 
9
- include Stubba
9
+ include Mocha
10
10
 
11
- def test_should_provide_hidden_version_of_method_name
11
+ def test_should_provide_hidden_version_of_method_name_starting_with_prefix
12
12
  method = ClassMethod.new(nil, :original_method_name)
13
- assert_equal '__stubba__original_method_name__stubba__', method.hidden_method
13
+ assert_match /^__stubba__/, method.hidden_method
14
14
  end
15
15
 
16
- def test_should_provide_hidden_version_of_method_name_with_question_mark
17
- method = ClassMethod.new(nil, :original_method_name?)
18
- assert_equal '__stubba__original_method_name_question_mark__stubba__', method.hidden_method
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 test_should_provide_hidden_version_of_method_name_with_exclamation_mark
22
- method = ClassMethod.new(nil, :original_method_name!)
23
- assert_equal '__stubba__original_method_name_exclamation_mark__stubba__', method.hidden_method
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
- expectation = Expectation.new(:expected_method)
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 = Expectation.new(:expected_method).with()
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 = Expectation.new(:expected_method).with()
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 = Expectation.new(:expected_method).with(1, 2, 3)
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 = Expectation.new(:expected_method).with() {|x, y, z| x + y == z}
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
- expectation = Expectation.new(:expected_method)
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 = Expectation.new(:expected_method).with(1, 2, 3)
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 = Expectation.new(:expected_method).with(1, 2)
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 = Expectation.new(:expected_method).with(1, 2, 3)
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 = Expectation.new(:expected_method).with() {|x, y, z| x + y == z}
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
- expectation.invoke() { yielded = true }
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 = Expectation.new(:expected_method).yields
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 = Expectation.new(:expected_method).yields(*parameters_for_yield)
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 = Expectation.new(:expected_method).returns(99)
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 = Expectation.new(:expected_method)
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
- expectation = Expectation.new(:expected_method).returns(lambda { 99 })
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 = Expectation.new(:expected_method).raises
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 = Expectation.new(:expected_method).raises(exception)
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 = Expectation.new(:expected_method).raises(Exception)
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 = Expectation.new(:expected_method).raises(Exception, exception_msg)
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 = Expectation.new(:expected_method)
179
+ expectation = new_expectation
134
180
  expectation.invoke
135
- assert_nothing_raised(Test::Unit::AssertionFailedError) {
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 = Expectation.new(:expected_method).at_least_once
187
+ expectation = new_expectation.at_least_once
142
188
  3.times {expectation.invoke}
143
- assert_nothing_raised(Test::Unit::AssertionFailedError) {
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 = Expectation.new(:expected_method).with(1, 2, 3).at_least_once
150
- e = assert_raise(Test::Unit::AssertionFailedError) {
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 = Expectation.new(:expected_method).times(2)
203
+ expectation = new_expectation.times(2)
158
204
  2.times {expectation.invoke}
159
- assert_nothing_raised(Test::Unit::AssertionFailedError) {
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 = Expectation.new(:expected_method)
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 = Expectation.new(:expected_method).times(2)
219
+ expectation = new_expectation.times(2)
174
220
  1.times {expectation.invoke}
175
- e = assert_raise(Test::Unit::AssertionFailedError) {
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 = Expectation.new(:expected_method).times(2)
228
+ expectation = new_expectation.times(2)
183
229
  3.times {expectation.invoke}
184
- assert_raise(Test::Unit::AssertionFailedError) {
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 = Expectation.new(:expected_method)
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(Test::Unit::AssertionFailedError) {
201
- expectation.verify { |x| yielded = true }
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(Test::Unit::AssertionFailedError) {
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 = Expectation.new(:expected_method).with(*options)
222
- exception = assert_raise(Test::Unit::AssertionFailedError) {
266
+ expectation = new_expectation.with(*options)
267
+ exception = assert_raise(ExpectationError) {
223
268
  expectation.verify
224
269
  }
225
- assert exception.message.include?(expectation.message)
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 = Expectation.new(:meth).with(*arguments)
231
- assert_equal ":meth(#{PrettyParameters.new(arguments).pretty})", expectation.message
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
- expectation = Expectation.new(:meth)
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(:meth)
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
- failed_expectation = MissingExpectation.new(:meth, @mock, [expectation]).with(1)
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
- missing_expectation = MissingExpectation.new(:meth, @mock, [expectation]).with(1)
265
- exception = assert_raise(Test::Unit::AssertionFailedError) { missing_expectation.verify }
266
- assert_equal "Unexpected message :meth(1) sent to #{@mock.mocha_inspect}\nSimilar expectations :meth(2)", exception.message
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
- failed_expectation = MissingExpectation.new(:other_meth, @mock, [expectation]).with(1)
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
- missing_expectation = MissingExpectation.new(:other_meth, @mock, [expectation]).with(1)
276
- exception = assert_raise(Test::Unit::AssertionFailedError) { missing_expectation.verify }
277
- assert_equal "Unexpected message :other_meth(1) sent to #{@mock.mocha_inspect}", exception.message
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