mocha 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. data/Rakefile +28 -10
  2. data/examples/stubba.rb +1 -1
  3. data/lib/mocha/auto_verify.rb +6 -6
  4. data/lib/mocha/deprecation.rb +22 -0
  5. data/lib/mocha/exception_raiser.rb +17 -0
  6. data/lib/mocha/expectation.rb +167 -84
  7. data/lib/mocha/infinite_range.rb +4 -6
  8. data/lib/mocha/inspect.rb +3 -1
  9. data/lib/mocha/is_a.rb +9 -0
  10. data/lib/mocha/missing_expectation.rb +27 -0
  11. data/lib/mocha/mock.rb +191 -5
  12. data/lib/mocha/multiple_yields.rb +20 -0
  13. data/lib/mocha/no_yields.rb +11 -0
  14. data/lib/mocha/object.rb +11 -1
  15. data/lib/mocha/parameter_matchers.rb +9 -0
  16. data/lib/mocha/parameter_matchers/all_of.rb +39 -0
  17. data/lib/mocha/parameter_matchers/any_of.rb +44 -0
  18. data/lib/mocha/parameter_matchers/anything.rb +30 -0
  19. data/lib/mocha/parameter_matchers/has_entry.rb +39 -0
  20. data/lib/mocha/parameter_matchers/has_key.rb +39 -0
  21. data/lib/mocha/parameter_matchers/has_value.rb +39 -0
  22. data/lib/mocha/parameter_matchers/includes.rb +37 -0
  23. data/lib/mocha/return_values.rb +31 -0
  24. data/lib/mocha/single_return_value.rb +24 -0
  25. data/lib/mocha/single_yield.rb +18 -0
  26. data/lib/mocha/standalone.rb +2 -0
  27. data/lib/mocha/stub.rb +18 -0
  28. data/lib/mocha/yield_parameters.rb +31 -0
  29. data/test/{mocha_acceptance_test.rb → acceptance/mocha_acceptance_test.rb} +1 -1
  30. data/test/acceptance/mocked_methods_dispatch_acceptance_test.rb +72 -0
  31. data/test/acceptance/parameter_matcher_acceptance_test.rb +63 -0
  32. data/test/{standalone_acceptance_test.rb → acceptance/standalone_acceptance_test.rb} +22 -1
  33. data/test/{stubba_acceptance_test.rb → acceptance/stubba_acceptance_test.rb} +1 -1
  34. data/test/deprecation_disabler.rb +15 -0
  35. data/test/{mocha_test_result_integration_test.rb → integration/mocha_test_result_integration_test.rb} +1 -1
  36. data/test/{stubba_integration_test.rb → integration/stubba_integration_test.rb} +1 -1
  37. data/test/{stubba_test_result_integration_test.rb → integration/stubba_test_result_integration_test.rb} +1 -1
  38. data/test/test_helper.rb +8 -0
  39. data/test/test_runner.rb +31 -0
  40. data/test/{mocha → unit}/any_instance_method_test.rb +0 -0
  41. data/test/unit/array_inspect_test.rb +16 -0
  42. data/test/{mocha → unit}/auto_verify_test.rb +0 -0
  43. data/test/{mocha → unit}/central_test.rb +0 -0
  44. data/test/{mocha → unit}/class_method_test.rb +0 -0
  45. data/test/unit/date_time_inspect_test.rb +21 -0
  46. data/test/unit/expectation_raiser_test.rb +28 -0
  47. data/test/{mocha → unit}/expectation_test.rb +105 -63
  48. data/test/unit/hash_inspect_test.rb +16 -0
  49. data/test/{mocha → unit}/infinite_range_test.rb +8 -5
  50. data/test/{mocha → unit}/metaclass_test.rb +0 -0
  51. data/test/unit/missing_expectation_test.rb +51 -0
  52. data/test/unit/mock_test.rb +351 -0
  53. data/test/unit/multiple_yields_test.rb +18 -0
  54. data/test/unit/no_yield_test.rb +18 -0
  55. data/test/unit/object_inspect_test.rb +35 -0
  56. data/test/{mocha → unit}/object_test.rb +0 -0
  57. data/test/unit/parameter_matchers/all_of_test.rb +26 -0
  58. data/test/unit/parameter_matchers/any_of_test.rb +26 -0
  59. data/test/unit/parameter_matchers/anything_test.rb +21 -0
  60. data/test/unit/parameter_matchers/has_entry_test.rb +25 -0
  61. data/test/unit/parameter_matchers/has_key_test.rb +25 -0
  62. data/test/unit/parameter_matchers/has_value_test.rb +25 -0
  63. data/test/unit/parameter_matchers/includes_test.rb +25 -0
  64. data/test/unit/parameter_matchers/stub_matcher.rb +22 -0
  65. data/test/{mocha → unit}/pretty_parameters_test.rb +0 -0
  66. data/test/unit/return_values_test.rb +63 -0
  67. data/test/{mocha → unit}/setup_and_teardown_test.rb +0 -0
  68. data/test/unit/single_return_value_test.rb +33 -0
  69. data/test/unit/single_yield_test.rb +18 -0
  70. data/test/unit/string_inspect_test.rb +11 -0
  71. data/test/unit/stub_test.rb +24 -0
  72. data/test/unit/yield_parameters_test.rb +93 -0
  73. metadata +117 -73
  74. data/lib/mocha/mock_methods.rb +0 -122
  75. data/test/all_tests.rb +0 -75
  76. data/test/mocha/inspect_test.rb +0 -90
  77. data/test/mocha/mock_methods_test.rb +0 -235
  78. data/test/mocha/mock_test.rb +0 -84
@@ -0,0 +1,16 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha/inspect'
3
+
4
+ class HashInspectTest < Test::Unit::TestCase
5
+
6
+ def test_should_keep_spacing_between_key_value
7
+ hash = {:a => true}
8
+ assert_equal '{:a => true}', hash.mocha_inspect
9
+ end
10
+
11
+ def test_should_use_mocha_inspect_on_each_item
12
+ hash = {:a => 'mocha'}
13
+ assert_equal "{:a => 'mocha'}", hash.mocha_inspect
14
+ end
15
+
16
+ end
@@ -1,5 +1,6 @@
1
1
  require File.join(File.dirname(__FILE__), "..", "test_helper")
2
2
  require 'mocha/infinite_range'
3
+ require 'date'
3
4
 
4
5
  class InfiniteRangeTest < Test::Unit::TestCase
5
6
 
@@ -18,7 +19,7 @@ class InfiniteRangeTest < Test::Unit::TestCase
18
19
  end
19
20
 
20
21
  def test_should_be_human_readable_description_for_at_least
21
- assert_equal "at least 10", Range.at_least(10).to_s
22
+ assert_equal "at least 10", Range.at_least(10).mocha_inspect
22
23
  end
23
24
 
24
25
  def test_should_include_values_at_or_below_maximum
@@ -35,12 +36,14 @@ class InfiniteRangeTest < Test::Unit::TestCase
35
36
  end
36
37
 
37
38
  def test_should_be_human_readable_description_for_at_most
38
- assert_equal "at most 10", Range.at_most(10).to_s
39
+ assert_equal "at most 10", Range.at_most(10).mocha_inspect
39
40
  end
40
41
 
41
- def test_should_not_break_original_description
42
- assert_equal "1..10", (1..10).to_s
43
- assert_equal "1...10", (1...10).to_s
42
+ def test_should_be_same_as_standard_to_string
43
+ assert_equal (1..10).to_s, (1..10).mocha_inspect
44
+ assert_equal (1...10).to_s, (1...10).mocha_inspect
45
+ date_range = Range.new(Date.parse('2006-01-01'), Date.parse('2007-01-01'))
46
+ assert_equal date_range.to_s, date_range.mocha_inspect
44
47
  end
45
48
 
46
49
  def assert_false(condition)
File without changes
@@ -0,0 +1,51 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+
3
+ require 'mocha/missing_expectation'
4
+ require 'method_definer'
5
+
6
+ class MissingExpectationsTest < Test::Unit::TestCase
7
+
8
+ include Mocha
9
+
10
+ def new_expectation
11
+ Expectation.new(nil, :expected_method)
12
+ end
13
+
14
+ def test_should_find_expectations_to_the_same_method
15
+ expectation = new_expectation.with(1)
16
+ mock = Object.new
17
+ mock.define_instance_method(:expectations) { [expectation] }
18
+ failed_expectation = MissingExpectation.new(mock, :expected_method).with(2)
19
+ assert_equal [expectation], failed_expectation.similar_expectations
20
+ end
21
+
22
+ def test_should_report_similar_expectations
23
+ mock = Object.new
24
+ expectation_1 = new_expectation.with(1)
25
+ expectation_2 = new_expectation.with(2)
26
+ mock = Object.new
27
+ mock.define_instance_method(:expectations) { [expectation_1, expectation_2] }
28
+ missing_expectation = MissingExpectation.new(mock, :expected_method).with(3)
29
+ exception = assert_raise(ExpectationError) { missing_expectation.verify }
30
+ assert_equal "#{mock.mocha_inspect}.expected_method(3) - expected calls: 0, actual calls: 1\nSimilar expectations:\nexpected_method(1)\nexpected_method(2)", exception.message
31
+ end
32
+
33
+ def test_should_ignore_expectations_to_different_methods
34
+ expectation = new_expectation.with(1)
35
+ mock = Object.new
36
+ mock.define_instance_method(:expectations) { [expectation] }
37
+ failed_expectation = MissingExpectation.new(mock, :other_method).with(1)
38
+ assert failed_expectation.similar_expectations.empty?
39
+ end
40
+
41
+ def test_should_not_report_similar_expectations
42
+ expectation = new_expectation.with(1)
43
+ mock = Object.new
44
+ mock.define_instance_method(:expectations) { [expectation] }
45
+ mock.define_instance_method(:mocha_inspect) { 'mocha_inspect' }
46
+ missing_expectation = MissingExpectation.new(mock, :unexpected_method).with(1)
47
+ exception = assert_raise(ExpectationError) { missing_expectation.verify }
48
+ assert_equal "mocha_inspect.unexpected_method(1) - expected calls: 0, actual calls: 1", exception.message
49
+ end
50
+
51
+ end
@@ -0,0 +1,351 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha/mock'
3
+ require 'mocha/expectation_error'
4
+ require 'set'
5
+
6
+ class MockTest < Test::Unit::TestCase
7
+
8
+ include Mocha
9
+
10
+ def test_should_set_single_expectation
11
+ mock = Mock.new
12
+ mock.expects(:method1).returns(1)
13
+ assert_nothing_raised(ExpectationError) do
14
+ assert_equal 1, mock.method1
15
+ end
16
+ end
17
+
18
+ def test_should_build_and_store_expectations
19
+ mock = Mock.new
20
+ expectation = mock.expects(:method1)
21
+ assert_not_nil expectation
22
+ assert_equal [expectation], mock.expectations
23
+ end
24
+
25
+ def test_should_not_stub_everything_by_default
26
+ mock = Mock.new
27
+ assert_equal false, mock.stub_everything
28
+ end
29
+
30
+ def test_should_stub_everything
31
+ mock = Mock.new(stub_everything = true)
32
+ assert_equal true, mock.stub_everything
33
+ end
34
+
35
+ def test_should_display_object_id_for_mocha_inspect_if_mock_has_no_name
36
+ mock = Mock.new
37
+ assert_match Regexp.new("^#<Mock:0x[0-9A-Fa-f]{1,8}>$"), mock.mocha_inspect
38
+ end
39
+
40
+ def test_should_display_name_for_mocha_inspect_if_mock_has_name
41
+ mock = Mock.new(false, 'named_mock')
42
+ assert_equal "#<Mock:named_mock>", mock.mocha_inspect
43
+ end
44
+
45
+ def test_should_display_object_id_for_inspect_if_mock_has_no_name
46
+ mock = Mock.new
47
+ assert_match Regexp.new("^#<Mock:0x[0-9A-Fa-f]{1,8}>$"), mock.inspect
48
+ end
49
+
50
+ def test_should_display_name_for_inspect_if_mock_has_name
51
+ mock = Mock.new(false, 'named_mock')
52
+ assert_equal "#<Mock:named_mock>", mock.inspect
53
+ end
54
+
55
+ def test_should_be_able_to_extend_mock_object_with_module
56
+ mock = Mock.new
57
+ assert_nothing_raised(ExpectationError) { mock.extend(Module.new) }
58
+ end
59
+
60
+ def test_should_be_equal
61
+ mock = Mock.new
62
+ assert_equal true, mock.eql?(mock)
63
+ end
64
+
65
+ def test_should_be_able_to_mock_standard_object_methods
66
+ mock = Mock.new
67
+ object_methods = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject { |m| m =~ /^__.*__$/ }.sort
68
+ object_methods.each { |method| mock.__expects__(method.to_sym).returns(method) }
69
+ object_methods.each { |method| assert_equal method, mock.__send__(method.to_sym) }
70
+ assert_nothing_raised(ExpectationError) { mock.verify }
71
+ end
72
+
73
+ def test_should_be_able_to_stub_standard_object_methods
74
+ mock = Mock.new
75
+ object_methods = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject { |m| m =~ /^__.*__$/ }.sort
76
+ object_methods.each { |method| mock.__stubs__(method.to_sym).returns(method) }
77
+ object_methods.each { |method| assert_equal method, mock.__send__(method.to_sym) }
78
+ end
79
+
80
+ def test_should_create_and_add_expectations
81
+ mock = Mock.new
82
+ expectation1 = mock.expects(:method1)
83
+ expectation2 = mock.expects(:method2)
84
+ assert_equal [expectation1, expectation2].to_set, mock.expectations.to_set
85
+ end
86
+
87
+ def test_should_pass_backtrace_into_expectation
88
+ mock = Mock.new
89
+ backtrace = Object.new
90
+ expectation = mock.expects(:method1, backtrace)
91
+ assert_equal backtrace, expectation.backtrace
92
+ end
93
+
94
+ def test_should_pass_backtrace_into_stub
95
+ mock = Mock.new
96
+ backtrace = Object.new
97
+ stub = mock.stubs(:method1, backtrace)
98
+ assert_equal backtrace, stub.backtrace
99
+ end
100
+
101
+ def test_should_create_and_add_stubs
102
+ mock = Mock.new
103
+ stub1 = mock.stubs(:method1)
104
+ stub2 = mock.stubs(:method2)
105
+ assert_equal [stub1, stub2].to_set, mock.expectations.to_set
106
+ end
107
+
108
+ def test_should_find_matching_expectation
109
+ mock = Mock.new
110
+ expectation1 = mock.expects(:my_method).with(:argument1, :argument2)
111
+ expectation2 = mock.expects(:my_method).with(:argument3, :argument4)
112
+ assert_same expectation2, mock.matching_expectation(:my_method, :argument3, :argument4)
113
+ end
114
+
115
+ def test_should_find_most_recent_matching_expectation
116
+ mock = Mock.new
117
+ expectation1 = mock.expects(:my_method).with(:argument1, :argument2)
118
+ expectation2 = mock.expects(:my_method).with(:argument1, :argument2)
119
+ assert_same expectation2, mock.matching_expectation(:my_method, :argument1, :argument2)
120
+ end
121
+
122
+ def test_should_invoke_expectation_and_return_result
123
+ mock = Mock.new
124
+ mock.expects(:my_method).returns(:result)
125
+ result = mock.my_method
126
+ assert_equal :result, result
127
+ end
128
+
129
+ def test_should_not_raise_error_if_stubbing_everything
130
+ mock = Mock.new(stub_everything = true)
131
+ result = nil
132
+ assert_nothing_raised(ExpectationError) do
133
+ result = mock.unexpected_method
134
+ end
135
+ assert_nil result
136
+ end
137
+
138
+ def test_should_raise_no_method_error
139
+ mock = Mock.new
140
+ assert_raise(NoMethodError) do
141
+ mock.super_method_missing(nil)
142
+ end
143
+ end
144
+
145
+ def test_should_raise_assertion_error_for_unexpected_method_call
146
+ mock = Mock.new
147
+ error = assert_raise(ExpectationError) do
148
+ mock.unexpected_method_called(:my_method, :argument1, :argument2)
149
+ end
150
+ assert_match /my_method/, error.message
151
+ assert_match /argument1/, error.message
152
+ assert_match /argument2/, error.message
153
+ end
154
+
155
+ def test_should_indicate_unexpected_method_called
156
+ mock = Mock.new
157
+ class << mock
158
+ attr_accessor :symbol, :arguments
159
+ def unexpected_method_called(symbol, *arguments)
160
+ self.symbol, self.arguments = symbol, arguments
161
+ end
162
+ end
163
+ mock.my_method(:argument1, :argument2)
164
+ assert_equal :my_method, mock.symbol
165
+ assert_equal [:argument1, :argument2], mock.arguments
166
+ end
167
+
168
+ def test_should_call_method_missing_for_parent
169
+ mock = Mock.new
170
+ class << mock
171
+ attr_accessor :symbol, :arguments
172
+ def super_method_missing(symbol, *arguments, &block)
173
+ self.symbol, self.arguments = symbol, arguments
174
+ end
175
+ end
176
+ mock.my_method(:argument1, :argument2)
177
+ assert_equal :my_method, mock.symbol
178
+ assert_equal [:argument1, :argument2], mock.arguments
179
+ end
180
+
181
+ def test_should_verify_that_all_expectations_have_been_fulfilled
182
+ mock = Mock.new
183
+ mock.expects(:method1)
184
+ mock.expects(:method2)
185
+ mock.method1
186
+ assert_raise(ExpectationError) do
187
+ mock.verify
188
+ end
189
+ end
190
+
191
+ def test_should_report_possible_expectations
192
+ mock = Mock.new
193
+ mock.expects(:expected_method).with(1)
194
+ exception = assert_raise(ExpectationError) { mock.expected_method(2) }
195
+ assert_equal "#{mock.mocha_inspect}.expected_method(2) - expected calls: 0, actual calls: 1\nSimilar expectations:\nexpected_method(1)", exception.message
196
+ end
197
+
198
+ def test_should_pass_block_through_to_expectations_verify_method
199
+ mock = Mock.new
200
+ expected_expectation = mock.expects(:method1)
201
+ mock.method1
202
+ expectations = []
203
+ mock.verify() { |expectation| expectations << expectation }
204
+ assert_equal [expected_expectation], expectations
205
+ end
206
+
207
+ def test_should_yield_supplied_parameters_to_block
208
+ mock = Mock.new
209
+ parameters_for_yield = [1, 2, 3]
210
+ mock.expects(:method1).yields(*parameters_for_yield)
211
+ yielded_parameters = nil
212
+ mock.method1() { |*parameters| yielded_parameters = parameters }
213
+ assert_equal parameters_for_yield, yielded_parameters
214
+ end
215
+
216
+ def test_should_set_up_multiple_expectations_with_return_values
217
+ mock = Mock.new
218
+ mock.expects(:method1 => :result1, :method2 => :result2)
219
+ assert_equal :result1, mock.method1
220
+ assert_equal :result2, mock.method2
221
+ end
222
+
223
+ def test_should_set_up_multiple_stubs_with_return_values
224
+ mock = Mock.new
225
+ mock.stubs(:method1 => :result1, :method2 => :result2)
226
+ assert_equal :result1, mock.method1
227
+ assert_equal :result2, mock.method2
228
+ end
229
+
230
+ def test_should_keep_returning_specified_value_for_stubs
231
+ mock = Mock.new
232
+ mock.stubs(:method1).returns(1)
233
+ assert_equal 1, mock.method1
234
+ assert_equal 1, mock.method1
235
+ end
236
+
237
+ def test_should_keep_returning_specified_value_for_expects
238
+ mock = Mock.new
239
+ mock.expects(:method1).times(2).returns(1)
240
+ assert_equal 1, mock.method1
241
+ assert_equal 1, mock.method1
242
+ end
243
+
244
+ def test_should_match_most_recent_call_to_expects
245
+ mock = Mock.new
246
+ mock.expects(:method1).returns(0)
247
+ mock.expects(:method1).returns(1)
248
+ assert_equal 1, mock.method1
249
+ end
250
+
251
+ def test_should_match_most_recent_call_to_stubs
252
+ mock = Mock.new
253
+ mock.stubs(:method1).returns(0)
254
+ mock.stubs(:method1).returns(1)
255
+ assert_equal 1, mock.method1
256
+ end
257
+
258
+ def test_should_match_most_recent_call_to_stubs_or_expects
259
+ mock = Mock.new
260
+ mock.stubs(:method1).returns(0)
261
+ mock.expects(:method1).returns(1)
262
+ assert_equal 1, mock.method1
263
+ end
264
+
265
+ def test_should_match_most_recent_call_to_expects_or_stubs
266
+ mock = Mock.new
267
+ mock.expects(:method1).returns(0)
268
+ mock.stubs(:method1).returns(1)
269
+ assert_equal 1, mock.method1
270
+ end
271
+
272
+ def test_should_respond_to_expected_method
273
+ mock = Mock.new
274
+ mock.expects(:method1)
275
+ assert_equal true, mock.respond_to?(:method1)
276
+ end
277
+
278
+ def test_should_not_respond_to_unexpected_method
279
+ mock = Mock.new
280
+ assert_equal false, mock.respond_to?(:method1)
281
+ end
282
+
283
+ def test_should_respond_to_methods_which_the_responder_does_responds_to
284
+ instance = Class.new do
285
+ define_method(:respond_to?) { true }
286
+ end.new
287
+ mock = Mock.new
288
+ mock.responds_like(instance)
289
+ assert_equal true, mock.respond_to?(:invoked_method)
290
+ end
291
+
292
+ def test_should_not_respond_to_methods_which_the_responder_does_not_responds_to
293
+ instance = Class.new do
294
+ define_method(:respond_to?) { false }
295
+ end.new
296
+ mock = Mock.new
297
+ mock.responds_like(instance)
298
+ assert_equal false, mock.respond_to?(:invoked_method)
299
+ end
300
+
301
+ def test_should_return_itself_to_allow_method_chaining
302
+ mock = Mock.new
303
+ assert_same mock.responds_like(Object.new), mock
304
+ end
305
+
306
+ def test_should_not_raise_no_method_error_if_mock_is_not_restricted_to_respond_like_a_responder
307
+ instance = Class.new do
308
+ define_method(:respond_to?) { true }
309
+ end.new
310
+ mock = Mock.new
311
+ mock.stubs(:invoked_method)
312
+ assert_nothing_raised(NoMethodError) { mock.invoked_method }
313
+ end
314
+
315
+ def test_should_not_raise_no_method_error_if_responder_does_respond_to_invoked_method
316
+ instance = Class.new do
317
+ define_method(:respond_to?) { true }
318
+ end.new
319
+ mock = Mock.new
320
+ mock.responds_like(instance)
321
+ mock.stubs(:invoked_method)
322
+ assert_nothing_raised(NoMethodError) { mock.invoked_method }
323
+ end
324
+
325
+ def test_should_raise_no_method_error_if_responder_does_not_respond_to_invoked_method
326
+ instance = Class.new do
327
+ define_method(:respond_to?) { false }
328
+ define_method(:mocha_inspect) { 'mocha_inspect' }
329
+ end.new
330
+ mock = Mock.new
331
+ mock.responds_like(instance)
332
+ mock.stubs(:invoked_method)
333
+ assert_raises(NoMethodError) { mock.invoked_method }
334
+ end
335
+
336
+ def test_should_raise_no_method_error_with_message_indicating_that_mock_is_constrained_to_respond_like_responder
337
+ instance = Class.new do
338
+ define_method(:respond_to?) { false }
339
+ define_method(:mocha_inspect) { 'mocha_inspect' }
340
+ end.new
341
+ mock = Mock.new
342
+ mock.responds_like(instance)
343
+ mock.stubs(:invoked_method)
344
+ begin
345
+ mock.invoked_method
346
+ rescue NoMethodError => e
347
+ assert_match /which responds like mocha_inspect/, e.message
348
+ end
349
+ end
350
+
351
+ end