mocha 0.12.7 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. data/.yardopts +22 -0
  2. data/COPYING.md +3 -0
  3. data/README.md +230 -0
  4. data/{RELEASE.rdoc → RELEASE.md} +61 -47
  5. data/Rakefile +45 -37
  6. data/build-matrix.rb +16 -4
  7. data/lib/mocha/api.rb +13 -16
  8. data/lib/mocha/class_method.rb +7 -3
  9. data/lib/mocha/class_methods.rb +63 -0
  10. data/lib/mocha/debug.rb +11 -0
  11. data/lib/mocha/deprecation.rb +1 -1
  12. data/lib/mocha/exception_raiser.rb +1 -1
  13. data/lib/mocha/expectation_error.rb +5 -0
  14. data/lib/mocha/expectation_error_factory.rb +19 -0
  15. data/lib/mocha/hooks.rb +43 -0
  16. data/lib/mocha/integration/assertion_counter.rb +13 -0
  17. data/lib/mocha/integration/mini_test/adapter.rb +54 -0
  18. data/lib/mocha/integration/mini_test/exception_translation.rb +0 -6
  19. data/lib/mocha/integration/mini_test/nothing.rb +19 -0
  20. data/lib/mocha/integration/mini_test/version_13.rb +32 -25
  21. data/lib/mocha/integration/mini_test/version_140.rb +32 -26
  22. data/lib/mocha/integration/mini_test/version_141.rb +40 -34
  23. data/lib/mocha/integration/mini_test/version_142_to_172.rb +40 -34
  24. data/lib/mocha/integration/mini_test/version_200.rb +41 -35
  25. data/lib/mocha/integration/mini_test/version_201_to_222.rb +41 -35
  26. data/lib/mocha/integration/mini_test/version_2110_to_2111.rb +49 -43
  27. data/lib/mocha/integration/mini_test/version_2112_to_320.rb +53 -47
  28. data/lib/mocha/integration/mini_test/version_230_to_2101.rb +43 -37
  29. data/lib/mocha/integration/mini_test.rb +42 -51
  30. data/lib/mocha/integration/monkey_patcher.rb +18 -0
  31. data/lib/mocha/integration/test_unit/adapter.rb +51 -0
  32. data/lib/mocha/integration/test_unit/gem_version_200.rb +36 -29
  33. data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +36 -29
  34. data/lib/mocha/integration/test_unit/gem_version_203_to_220.rb +36 -29
  35. data/lib/mocha/integration/test_unit/gem_version_230_to_250.rb +65 -0
  36. data/lib/mocha/integration/test_unit/nothing.rb +19 -0
  37. data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +36 -29
  38. data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +37 -30
  39. data/lib/mocha/integration/test_unit.rb +38 -45
  40. data/lib/mocha/integration.rb +6 -38
  41. data/lib/mocha/method_matcher.rb +1 -1
  42. data/lib/mocha/module_methods.rb +14 -0
  43. data/lib/mocha/{object.rb → object_methods.rb} +0 -83
  44. data/lib/mocha/setup.rb +10 -0
  45. data/lib/mocha/standalone.rb +3 -0
  46. data/lib/mocha/version.rb +1 -1
  47. data/lib/mocha.rb +6 -2
  48. data/lib/mocha_standalone.rb +3 -1
  49. data/mocha.gemspec +16 -9
  50. data/test/acceptance/bug_18914_test.rb +1 -1
  51. data/test/acceptance/bug_21465_test.rb +1 -1
  52. data/test/acceptance/bug_21563_test.rb +1 -1
  53. data/test/acceptance/exception_rescue_test.rb +1 -1
  54. data/test/acceptance/expectations_on_multiple_methods_test.rb +1 -1
  55. data/test/acceptance/expected_invocation_count_test.rb +1 -1
  56. data/test/acceptance/failure_messages_test.rb +1 -1
  57. data/test/acceptance/issue_65_test.rb +1 -1
  58. data/test/acceptance/issue_70_test.rb +1 -1
  59. data/test/acceptance/mocha_example_test.rb +1 -1
  60. data/test/acceptance/mocha_test_result_test.rb +1 -1
  61. data/test/acceptance/mock_test.rb +1 -1
  62. data/test/acceptance/mock_with_initializer_block_test.rb +1 -1
  63. data/test/acceptance/mocked_methods_dispatch_test.rb +1 -1
  64. data/test/acceptance/multiple_expectations_failure_message_test.rb +1 -1
  65. data/test/acceptance/optional_parameters_test.rb +1 -1
  66. data/test/acceptance/parameter_matcher_test.rb +1 -1
  67. data/test/acceptance/partial_mocks_test.rb +1 -1
  68. data/test/acceptance/raise_exception_test.rb +1 -1
  69. data/test/acceptance/return_value_test.rb +1 -1
  70. data/test/acceptance/sequence_test.rb +1 -1
  71. data/test/acceptance/states_test.rb +1 -1
  72. data/test/acceptance/stub_any_instance_method_test.rb +1 -1
  73. data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +1 -1
  74. data/test/acceptance/stub_class_method_defined_on_class_test.rb +1 -1
  75. data/test/acceptance/stub_class_method_defined_on_module_test.rb +1 -1
  76. data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +1 -1
  77. data/test/acceptance/stub_everything_test.rb +1 -1
  78. data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +1 -1
  79. data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +1 -1
  80. data/test/acceptance/stub_instance_method_defined_on_class_test.rb +1 -1
  81. data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +1 -1
  82. data/test/acceptance/stub_instance_method_defined_on_module_test.rb +1 -1
  83. data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +1 -1
  84. data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +1 -1
  85. data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +1 -1
  86. data/test/acceptance/stub_module_method_test.rb +1 -1
  87. data/test/acceptance/stub_test.rb +1 -1
  88. data/test/acceptance/stubba_example_test.rb +1 -1
  89. data/test/acceptance/stubba_test_result_test.rb +1 -1
  90. data/test/acceptance/stubbing_error_backtrace_test.rb +1 -1
  91. data/test/acceptance/stubbing_frozen_object_test.rb +1 -1
  92. data/test/acceptance/stubbing_method_accepting_block_parameter_test.rb +1 -1
  93. data/test/acceptance/stubbing_method_unnecessarily_test.rb +1 -1
  94. data/test/acceptance/stubbing_nil_test.rb +1 -1
  95. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +1 -1
  96. data/test/acceptance/stubbing_non_existent_class_method_test.rb +1 -1
  97. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +1 -1
  98. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +1 -1
  99. data/test/acceptance/stubbing_non_public_class_method_test.rb +1 -1
  100. data/test/acceptance/stubbing_non_public_instance_method_test.rb +1 -1
  101. data/test/acceptance/stubbing_on_non_mock_object_test.rb +1 -1
  102. data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +3 -3
  103. data/test/acceptance/throw_test.rb +1 -1
  104. data/test/acceptance/unstubbing_test.rb +1 -1
  105. data/test/integration/mini_test_test.rb +9 -0
  106. data/test/integration/shared_tests.rb +174 -0
  107. data/test/integration/test_unit_test.rb +9 -0
  108. data/test/mini_test_result.rb +0 -1
  109. data/test/test_runner.rb +14 -14
  110. data/test/unit/class_methods_test.rb +40 -0
  111. data/test/unit/method_matcher_test.rb +5 -0
  112. data/test/unit/mock_test.rb +6 -0
  113. data/test/unit/module_methods_test.rb +19 -0
  114. data/test/unit/object_methods_test.rb +45 -0
  115. metadata +43 -42
  116. data/COPYING.rdoc +0 -3
  117. data/README.rdoc +0 -56
  118. data/examples/misc.rb +0 -43
  119. data/examples/mocha.rb +0 -25
  120. data/examples/stubba.rb +0 -64
  121. data/lib/mocha/integration/mini_test/assertion_counter.rb +0 -23
  122. data/lib/mocha/integration/mini_test/version_330_to_410.rb +0 -65
  123. data/lib/mocha/integration/test_unit/assertion_counter.rb +0 -23
  124. data/lib/mocha/integration/test_unit/gem_version_230_to_252.rb +0 -58
  125. data/lib/mocha/options.rb +0 -1
  126. data/test/acceptance/api_test.rb +0 -139
  127. data/test/acceptance/minitest_test.rb +0 -162
  128. data/test/unit/object_test.rb +0 -92
  129. /data/{MIT-LICENSE.rdoc → MIT-LICENSE.md} +0 -0
@@ -214,6 +214,12 @@ class MockTest < Test::Unit::TestCase
214
214
  assert_equal true, mock.respond_to?(:method1)
215
215
  end
216
216
 
217
+ def test_should_respond_to_expected_method_as_string
218
+ mock = build_mock
219
+ mock.expects(:method1)
220
+ assert_equal true, mock.respond_to?('method1')
221
+ end
222
+
217
223
  def test_should_not_respond_to_unexpected_method
218
224
  mock = build_mock
219
225
  assert_equal false, mock.respond_to?(:method1)
@@ -0,0 +1,19 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+ require 'mocha/module_methods'
3
+ require 'mocha/object_methods'
4
+
5
+ class ModuleMethodsTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @module = Module.new.extend(Mocha::ModuleMethods, Mocha::ObjectMethods)
9
+ end
10
+
11
+ def test_should_use_stubba_module_method_for_module
12
+ assert_equal Mocha::ModuleMethod, @module.stubba_method
13
+ end
14
+
15
+ def test_should_stub_self_for_module
16
+ assert_equal @module, @module.stubba_object
17
+ end
18
+
19
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+ require 'mocha/object_methods'
3
+ require 'mocha/mock'
4
+
5
+ class ObjectMethodsTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @object = Object.new.extend(Mocha::ObjectMethods)
9
+ end
10
+
11
+ def test_should_build_mocha_referring_to_self
12
+ mocha = @object.mocha
13
+ assert_not_nil mocha
14
+ assert mocha.is_a?(Mocha::Mock)
15
+ assert_equal @object.mocha_inspect, mocha.mocha_inspect
16
+ end
17
+
18
+ def test_should_reuse_existing_mocha
19
+ mocha_1 = @object.mocha
20
+ mocha_2 = @object.mocha
21
+ assert_equal mocha_1, mocha_2
22
+ end
23
+
24
+ def test_should_reset_mocha
25
+ assert_nil @object.reset_mocha
26
+ end
27
+
28
+ def test_should_use_stubba_instance_method_for_object
29
+ assert_equal Mocha::InstanceMethod, Object.new.stubba_method
30
+ end
31
+
32
+ def test_should_stub_self_for_object
33
+ assert_equal @object, @object.stubba_object
34
+ end
35
+
36
+ def test_nobody_expects_the_spanish_inquisition
37
+ assert_raise(Mocha::ExpectationError) { @object.expects(:the_spanish_inquisition) }
38
+ end
39
+
40
+ def test_should_alias_object_method
41
+ klass = Class.new { def self.method_x; end }
42
+ assert_equal klass._method(:method_x), klass.method(:method_x)
43
+ end
44
+
45
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mocha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.7
4
+ version: 0.13.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-10 00:00:00.000000000 Z
12
+ date: 2012-11-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: metaclass
16
- requirement: &70269211293480 !ruby/object:Gem::Requirement
16
+ requirement: &70267336106140 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.0.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70269211293480
24
+ version_requirements: *70267336106140
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70269211292160 !ruby/object:Gem::Requirement
27
+ requirement: &70267336104820 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70269211292160
35
+ version_requirements: *70267336104820
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: introspection
38
- requirement: &70269211291700 !ruby/object:Gem::Requirement
38
+ requirement: &70267336104360 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.0.1
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70269211291700
46
+ version_requirements: *70267336104360
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: yard
49
- requirement: &70269211291320 !ruby/object:Gem::Requirement
49
+ requirement: &70267336103960 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,38 +54,34 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70269211291320
57
+ version_requirements: *70267336103960
58
58
  - !ruby/object:Gem::Dependency
59
- name: rdoc
60
- requirement: &70269211290860 !ruby/object:Gem::Requirement
59
+ name: redcarpet
60
+ requirement: &70267336103420 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
- - - ! '>='
63
+ - - ~>
64
64
  - !ruby/object:Gem::Version
65
- version: '0'
65
+ version: '1'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70269211290860
68
+ version_requirements: *70267336103420
69
69
  description: Mocking and stubbing library with JMock/SchMock syntax, which allows
70
70
  mocking and stubbing of methods on real (non-mock) classes.
71
71
  email: mocha-developer@googlegroups.com
72
72
  executables: []
73
73
  extensions: []
74
- extra_rdoc_files:
75
- - README.rdoc
76
- - COPYING.rdoc
74
+ extra_rdoc_files: []
77
75
  files:
78
76
  - .gemtest
79
- - COPYING.rdoc
77
+ - .yardopts
78
+ - COPYING.md
80
79
  - Gemfile
81
- - MIT-LICENSE.rdoc
82
- - README.rdoc
83
- - RELEASE.rdoc
80
+ - MIT-LICENSE.md
81
+ - README.md
82
+ - RELEASE.md
84
83
  - Rakefile
85
84
  - build-matrix.rb
86
- - examples/misc.rb
87
- - examples/mocha.rb
88
- - examples/stubba.rb
89
85
  - gemfiles/Gemfile.minitest.1.3.0
90
86
  - gemfiles/Gemfile.minitest.1.4.0
91
87
  - gemfiles/Gemfile.minitest.1.4.1
@@ -110,20 +106,25 @@ files:
110
106
  - lib/mocha/central.rb
111
107
  - lib/mocha/change_state_side_effect.rb
112
108
  - lib/mocha/class_method.rb
109
+ - lib/mocha/class_methods.rb
113
110
  - lib/mocha/configuration.rb
111
+ - lib/mocha/debug.rb
114
112
  - lib/mocha/deprecation.rb
115
113
  - lib/mocha/exception_raiser.rb
116
114
  - lib/mocha/expectation.rb
117
115
  - lib/mocha/expectation_error.rb
118
116
  - lib/mocha/expectation_error_factory.rb
119
117
  - lib/mocha/expectation_list.rb
118
+ - lib/mocha/hooks.rb
120
119
  - lib/mocha/in_state_ordering_constraint.rb
121
120
  - lib/mocha/inspect.rb
122
121
  - lib/mocha/instance_method.rb
123
122
  - lib/mocha/integration.rb
123
+ - lib/mocha/integration/assertion_counter.rb
124
124
  - lib/mocha/integration/mini_test.rb
125
- - lib/mocha/integration/mini_test/assertion_counter.rb
125
+ - lib/mocha/integration/mini_test/adapter.rb
126
126
  - lib/mocha/integration/mini_test/exception_translation.rb
127
+ - lib/mocha/integration/mini_test/nothing.rb
127
128
  - lib/mocha/integration/mini_test/version_13.rb
128
129
  - lib/mocha/integration/mini_test/version_140.rb
129
130
  - lib/mocha/integration/mini_test/version_141.rb
@@ -133,13 +134,14 @@ files:
133
134
  - lib/mocha/integration/mini_test/version_2110_to_2111.rb
134
135
  - lib/mocha/integration/mini_test/version_2112_to_320.rb
135
136
  - lib/mocha/integration/mini_test/version_230_to_2101.rb
136
- - lib/mocha/integration/mini_test/version_330_to_410.rb
137
+ - lib/mocha/integration/monkey_patcher.rb
137
138
  - lib/mocha/integration/test_unit.rb
138
- - lib/mocha/integration/test_unit/assertion_counter.rb
139
+ - lib/mocha/integration/test_unit/adapter.rb
139
140
  - lib/mocha/integration/test_unit/gem_version_200.rb
140
141
  - lib/mocha/integration/test_unit/gem_version_201_to_202.rb
141
142
  - lib/mocha/integration/test_unit/gem_version_203_to_220.rb
142
- - lib/mocha/integration/test_unit/gem_version_230_to_252.rb
143
+ - lib/mocha/integration/test_unit/gem_version_230_to_250.rb
144
+ - lib/mocha/integration/test_unit/nothing.rb
143
145
  - lib/mocha/integration/test_unit/ruby_version_185_and_below.rb
144
146
  - lib/mocha/integration/test_unit/ruby_version_186_and_above.rb
145
147
  - lib/mocha/is_a.rb
@@ -148,11 +150,11 @@ files:
148
150
  - lib/mocha/mock.rb
149
151
  - lib/mocha/mockery.rb
150
152
  - lib/mocha/module_method.rb
153
+ - lib/mocha/module_methods.rb
151
154
  - lib/mocha/multiple_yields.rb
152
155
  - lib/mocha/names.rb
153
156
  - lib/mocha/no_yields.rb
154
- - lib/mocha/object.rb
155
- - lib/mocha/options.rb
157
+ - lib/mocha/object_methods.rb
156
158
  - lib/mocha/parameter_matchers.rb
157
159
  - lib/mocha/parameter_matchers/all_of.rb
158
160
  - lib/mocha/parameter_matchers/any_of.rb
@@ -179,6 +181,7 @@ files:
179
181
  - lib/mocha/pretty_parameters.rb
180
182
  - lib/mocha/return_values.rb
181
183
  - lib/mocha/sequence.rb
184
+ - lib/mocha/setup.rb
182
185
  - lib/mocha/single_return_value.rb
183
186
  - lib/mocha/single_yield.rb
184
187
  - lib/mocha/standalone.rb
@@ -191,7 +194,6 @@ files:
191
194
  - lib/mocha_standalone.rb
192
195
  - mocha.gemspec
193
196
  - test/acceptance/acceptance_test_helper.rb
194
- - test/acceptance/api_test.rb
195
197
  - test/acceptance/bug_18914_test.rb
196
198
  - test/acceptance/bug_21465_test.rb
197
199
  - test/acceptance/bug_21563_test.rb
@@ -201,7 +203,6 @@ files:
201
203
  - test/acceptance/failure_messages_test.rb
202
204
  - test/acceptance/issue_65_test.rb
203
205
  - test/acceptance/issue_70_test.rb
204
- - test/acceptance/minitest_test.rb
205
206
  - test/acceptance/mocha_example_test.rb
206
207
  - test/acceptance/mocha_test_result_test.rb
207
208
  - test/acceptance/mock_test.rb
@@ -250,6 +251,9 @@ files:
250
251
  - test/acceptance/unstubbing_test.rb
251
252
  - test/deprecation_disabler.rb
252
253
  - test/execution_point.rb
254
+ - test/integration/mini_test_test.rb
255
+ - test/integration/shared_tests.rb
256
+ - test/integration/test_unit_test.rb
253
257
  - test/method_definer.rb
254
258
  - test/mini_test_result.rb
255
259
  - test/simple_counter.rb
@@ -263,6 +267,7 @@ files:
263
267
  - test/unit/central_test.rb
264
268
  - test/unit/change_state_side_effect_test.rb
265
269
  - test/unit/class_method_test.rb
270
+ - test/unit/class_methods_test.rb
266
271
  - test/unit/configuration_test.rb
267
272
  - test/unit/date_time_inspect_test.rb
268
273
  - test/unit/exception_raiser_test.rb
@@ -273,10 +278,11 @@ files:
273
278
  - test/unit/method_matcher_test.rb
274
279
  - test/unit/mock_test.rb
275
280
  - test/unit/mockery_test.rb
281
+ - test/unit/module_methods_test.rb
276
282
  - test/unit/multiple_yields_test.rb
277
283
  - test/unit/no_yields_test.rb
278
284
  - test/unit/object_inspect_test.rb
279
- - test/unit/object_test.rb
285
+ - test/unit/object_methods_test.rb
280
286
  - test/unit/parameter_matchers/all_of_test.rb
281
287
  - test/unit/parameter_matchers/any_of_test.rb
282
288
  - test/unit/parameter_matchers/anything_test.rb
@@ -306,12 +312,7 @@ files:
306
312
  homepage: http://gofreerange.com/mocha/docs
307
313
  licenses: []
308
314
  post_install_message:
309
- rdoc_options:
310
- - --title
311
- - Mocha
312
- - --main
313
- - README.rdoc
314
- - --line-numbers
315
+ rdoc_options: []
315
316
  require_paths:
316
317
  - lib
317
318
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -322,7 +323,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
322
323
  version: '0'
323
324
  segments:
324
325
  - 0
325
- hash: 3599471528182065481
326
+ hash: -4192078905680069090
326
327
  required_rubygems_version: !ruby/object:Gem::Requirement
327
328
  none: false
328
329
  requirements:
@@ -336,4 +337,4 @@ signing_key:
336
337
  specification_version: 3
337
338
  summary: Mocking and stubbing library
338
339
  test_files: []
339
- has_rdoc:
340
+ has_rdoc: yard
data/COPYING.rdoc DELETED
@@ -1,3 +0,0 @@
1
- Copyright Revieworld Ltd. 2006
2
-
3
- You may use, copy and redistribute this library under the same terms as {Ruby itself}[http://www.ruby-lang.org/en/LICENSE.txt] or under the {file:MIT-LICENSE.rdoc MIT license}.
data/README.rdoc DELETED
@@ -1,56 +0,0 @@
1
- = Mocha {<img src="https://secure.travis-ci.org/freerange/mocha.png" />}[http://travis-ci.org/freerange/mocha]
2
-
3
- Mocha is a library for mocking and stubbing using a syntax like that of {JMock}[http://www.jmock.org].
4
-
5
- It can be used with many testing frameworks e.g. {Test::Unit}[http://www.ruby-doc.org/core/classes/Test/Unit.html], {RSpec}[http://rspec.info/], {test/spec}[http://chneukirchen.org/repos/testspec/README], {expectations}[http://expectations.rubyforge.org/], {Dust}[http://dust.rubyforge.org/], {MiniTest}[http://rubyforge.org/projects/bfts] and even {JtestR}[http://jtestr.codehaus.org/].
6
-
7
- Mocha provides a unified, simple and readable syntax for both traditional mocking and partial mocking.
8
-
9
- Mocha was harvested from projects at {Reevoo}[http://www.reevoo.com/] by me ({James}[http://jamesmead.org/]) and my (then) colleagues {Ben}[http://www.techbelly.com/], {Chris}[http://chrisroos.co.uk/] and {Paul}[http://po-ru.com/].
10
-
11
- == Installation
12
-
13
- Install the latest version of the gem with the following command...
14
-
15
- $ gem install mocha
16
-
17
- Note that if you are intending to use Mocha with Test::Unit or MiniTest, you should only load Mocha *after* loading the relevant test library...
18
-
19
- require "test/unit"
20
- require "mocha"
21
-
22
- If you're using Bundler, ensure the correct load order by not auto-requiring Mocha in the Gemfile and then later load it once you know the test library has been loaded...
23
-
24
- # Gemfile
25
- gem "mocha", :require => false
26
-
27
- # Elsewhere after Bundler has loaded gems
28
- require "test/unit"
29
- require "mocha"
30
-
31
- Note that versions 0.10.2, 0.10.3 & 0.11.0 included a bug. Please do not use these versions.
32
-
33
- Or install the {Rails}[http://www.rubyonrails.org/] plugin...
34
-
35
- $ script/plugin install git://github.com/freerange/mocha.git
36
-
37
- Note that versions 0.9.6 & 0.9.7 of the Rails plugin were broken. As of version 0.9.8, you need to explicitly load Mocha after the test framework e.g. by adding "require 'mocha'" at the bottom of test/test_helper.rb.
38
-
39
- == Examples
40
-
41
- * Quick Start - {file:misc.rb Usage Examples}
42
- * Traditional mocking - {file:mocha.rb Star Trek Example}
43
- * Setting expectations on real classes - {file:stubba.rb Order Example}
44
- * More examples on {James Mead's Blog}[http://jamesmead.org/blog/]
45
- * {Mailing List Archives}[http://groups.google.com/group/mocha-developer]
46
-
47
- == Links
48
-
49
- * {Source code}[http://github.com/freerange/mocha]
50
- * {Bug reports}[http://github.com/freerange/mocha/issues]
51
-
52
- == License
53
-
54
- Copyright Revieworld Ltd. 2006
55
-
56
- You may use, copy and redistribute this library under the same terms as {Ruby itself}[http://www.ruby-lang.org/en/LICENSE.txt] or under the {file:MIT-LICENSE.rdoc MIT license}.
data/examples/misc.rb DELETED
@@ -1,43 +0,0 @@
1
- require 'test/unit'
2
- require 'mocha'
3
-
4
- class MiscExampleTest < Test::Unit::TestCase
5
-
6
- def test_mocking_a_class_method
7
- product = Product.new
8
- Product.expects(:find).with(1).returns(product)
9
- assert_equal product, Product.find(1)
10
- end
11
-
12
- def test_mocking_an_instance_method_on_a_real_object
13
- product = Product.new
14
- product.expects(:save).returns(true)
15
- assert product.save
16
- end
17
-
18
- def test_stubbing_instance_methods_on_real_objects
19
- prices = [stub(:pence => 1000), stub(:pence => 2000)]
20
- product = Product.new
21
- product.stubs(:prices).returns(prices)
22
- assert_equal [1000, 2000], product.prices.collect {|p| p.pence}
23
- end
24
-
25
- def test_stubbing_an_instance_method_on_all_instances_of_a_class
26
- Product.any_instance.stubs(:name).returns('stubbed_name')
27
- product = Product.new
28
- assert_equal 'stubbed_name', product.name
29
- end
30
-
31
- def test_traditional_mocking
32
- object = mock()
33
- object.expects(:expected_method).with(:p1, :p2).returns(:result)
34
- assert_equal :result, object.expected_method(:p1, :p2)
35
- end
36
-
37
- def test_shortcuts
38
- object = stub(:method1 => :result1, :method2 => :result2)
39
- assert_equal :result1, object.method1
40
- assert_equal :result2, object.method2
41
- end
42
-
43
- end
data/examples/mocha.rb DELETED
@@ -1,25 +0,0 @@
1
- class Enterprise
2
-
3
- def initialize(dilithium)
4
- @dilithium = dilithium
5
- end
6
-
7
- def go(warp_factor)
8
- warp_factor.times { @dilithium.nuke(:anti_matter) }
9
- end
10
-
11
- end
12
-
13
- require 'test/unit'
14
- require 'mocha'
15
-
16
- class EnterpriseTest < Test::Unit::TestCase
17
-
18
- def test_should_boldly_go
19
- dilithium = mock()
20
- dilithium.expects(:nuke).with(:anti_matter).at_least_once # auto-verified at end of test
21
- enterprise = Enterprise.new(dilithium)
22
- enterprise.go(2)
23
- end
24
-
25
- end
data/examples/stubba.rb DELETED
@@ -1,64 +0,0 @@
1
- class Order
2
-
3
- attr_accessor :shipped_on
4
-
5
- def total_cost
6
- line_items.inject(0) { |total, line_item| total + line_item.price } + shipping_cost
7
- end
8
-
9
- def total_weight
10
- line_items.inject(0) { |total, line_item| total + line_item.weight }
11
- end
12
-
13
- def shipping_cost
14
- total_weight * 5 + 10
15
- end
16
-
17
- class << self
18
-
19
- def find_all
20
- # Database.connection.execute('select * from orders...
21
- end
22
-
23
- def number_shipped_since(date)
24
- find_all.select { |order| order.shipped_on > date }.length
25
- end
26
-
27
- def unshipped_value
28
- find_all.inject(0) { |total, order| order.shipped_on ? total : total + order.total_cost }
29
- end
30
-
31
- end
32
-
33
- end
34
-
35
- require 'test/unit'
36
- require 'mocha'
37
-
38
- class OrderTest < Test::Unit::TestCase
39
-
40
- # illustrates stubbing instance method
41
- def test_should_calculate_shipping_cost_based_on_total_weight
42
- order = Order.new
43
- order.stubs(:total_weight).returns(10)
44
- assert_equal 60, order.shipping_cost
45
- end
46
-
47
- # illustrates stubbing class method
48
- def test_should_count_number_of_orders_shipped_after_specified_date
49
- now = Time.now; week_in_secs = 7 * 24 * 60 * 60
50
- order_1 = Order.new; order_1.shipped_on = now - 1 * week_in_secs
51
- order_2 = Order.new; order_2.shipped_on = now - 3 * week_in_secs
52
- Order.stubs(:find_all).returns([order_1, order_2])
53
- assert_equal 1, Order.number_shipped_since(now - 2 * week_in_secs)
54
- end
55
-
56
- # illustrates stubbing instance method for all instances of a class
57
- def test_should_calculate_value_of_unshipped_orders
58
- Order.stubs(:find_all).returns([Order.new, Order.new, Order.new])
59
- Order.any_instance.stubs(:shipped_on).returns(nil)
60
- Order.any_instance.stubs(:total_cost).returns(10)
61
- assert_equal 30, Order.unshipped_value
62
- end
63
-
64
- end
@@ -1,23 +0,0 @@
1
- module Mocha
2
-
3
- module Integration
4
-
5
- module MiniTest
6
-
7
- class AssertionCounter
8
-
9
- def initialize(test_case)
10
- @test_case = test_case
11
- end
12
-
13
- def increment
14
- @test_case.assert(true)
15
- end
16
-
17
- end
18
-
19
- end
20
-
21
- end
22
-
23
- end
@@ -1,65 +0,0 @@
1
- require 'mocha/integration/mini_test/assertion_counter'
2
- require 'mocha/expectation_error'
3
-
4
- module Mocha
5
-
6
- module Integration
7
-
8
- module MiniTest
9
-
10
- module Version330To410
11
- def self.included(mod)
12
- $stderr.puts "Monkey patching MiniTest >= v3.3.0 <= v4.1.0" if $mocha_options['debug']
13
- end
14
- def run runner
15
- trap "INFO" do
16
- runner.report.each_with_index do |msg, i|
17
- warn "\n%3d) %s" % [i + 1, msg]
18
- end
19
- warn ''
20
- time = runner.start_time ? Time.now - runner.start_time : 0
21
- warn "Current Test: %s#%s %.2fs" % [self.class, self.__name__, time]
22
- runner.status $stderr
23
- end if ::MiniTest::Unit::TestCase::SUPPORTS_INFO_SIGNAL
24
- assertion_counter = AssertionCounter.new(self)
25
- result = ""
26
- begin
27
- begin
28
- @passed = nil
29
- self.before_setup
30
- self.setup
31
- self.after_setup
32
- self.run_test self.__name__
33
- mocha_verify(assertion_counter)
34
- result = "." unless io?
35
- @passed = true
36
- rescue *::MiniTest::Unit::TestCase::PASSTHROUGH_EXCEPTIONS
37
- raise
38
- rescue Exception => e
39
- @passed = false
40
- result = runner.puke self.class, self.__name__, Mocha::Integration::MiniTest.translate(e)
41
- ensure
42
- %w{ before_teardown teardown after_teardown }.each do |hook|
43
- begin
44
- self.send hook
45
- rescue *::MiniTest::Unit::TestCase::PASSTHROUGH_EXCEPTIONS
46
- raise
47
- rescue Exception => e
48
- @passed = false
49
- result = runner.puke self.class, self.__name__, Mocha::Integration::MiniTest.translate(e)
50
- end
51
- end
52
- trap 'INFO', 'DEFAULT' if ::MiniTest::Unit::TestCase::SUPPORTS_INFO_SIGNAL
53
- end
54
- ensure
55
- mocha_teardown
56
- end
57
- result
58
- end
59
- end
60
-
61
- end
62
-
63
- end
64
-
65
- end
@@ -1,23 +0,0 @@
1
- module Mocha
2
-
3
- module Integration
4
-
5
- module TestUnit
6
-
7
- class AssertionCounter
8
-
9
- def initialize(test_result)
10
- @test_result = test_result
11
- end
12
-
13
- def increment
14
- @test_result.add_assertion
15
- end
16
-
17
- end
18
-
19
- end
20
-
21
- end
22
-
23
- end