mocha 1.1.0 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (264) hide show
  1. checksums.yaml +5 -5
  2. data/.github/FUNDING.yml +1 -0
  3. data/.rubocop.yml +65 -0
  4. data/.rubocop_todo.yml +27 -0
  5. data/.yardopts +1 -0
  6. data/CONTRIBUTING.md +4 -9
  7. data/Gemfile +27 -0
  8. data/README.md +120 -28
  9. data/RELEASE.md +351 -1
  10. data/Rakefile +60 -43
  11. data/gemfiles/Gemfile.minitest.latest +1 -0
  12. data/gemfiles/Gemfile.test-unit.latest +1 -0
  13. data/lib/mocha/any_instance_method.rb +9 -74
  14. data/lib/mocha/api.rb +85 -69
  15. data/lib/mocha/argument_iterator.rb +4 -8
  16. data/lib/mocha/backtrace_filter.rb +1 -5
  17. data/lib/mocha/block_matcher.rb +31 -0
  18. data/lib/mocha/cardinality.rb +60 -49
  19. data/lib/mocha/central.rb +21 -12
  20. data/lib/mocha/change_state_side_effect.rb +0 -4
  21. data/lib/mocha/class_methods.rb +19 -20
  22. data/lib/mocha/configuration.rb +312 -47
  23. data/lib/mocha/debug.rb +3 -2
  24. data/lib/mocha/deprecation.rb +8 -11
  25. data/lib/mocha/detection/mini_test.rb +0 -2
  26. data/lib/mocha/detection/test_unit.rb +3 -5
  27. data/lib/mocha/error_with_filtered_backtrace.rb +13 -0
  28. data/lib/mocha/exception_raiser.rb +4 -6
  29. data/lib/mocha/expectation.rb +186 -95
  30. data/lib/mocha/expectation_error.rb +1 -1
  31. data/lib/mocha/expectation_error_factory.rb +0 -1
  32. data/lib/mocha/expectation_list.rb +7 -11
  33. data/lib/mocha/hooks.rb +1 -3
  34. data/lib/mocha/in_state_ordering_constraint.rb +0 -4
  35. data/lib/mocha/inspect.rb +30 -38
  36. data/lib/mocha/instance_method.rb +11 -8
  37. data/lib/mocha/integration/mini_test/adapter.rb +2 -4
  38. data/lib/mocha/integration/mini_test/exception_translation.rb +1 -1
  39. data/lib/mocha/integration/mini_test.rb +10 -31
  40. data/lib/mocha/integration/monkey_patcher.rb +8 -2
  41. data/lib/mocha/integration/test_unit/adapter.rb +5 -6
  42. data/lib/mocha/integration/test_unit.rb +10 -26
  43. data/lib/mocha/invocation.rb +73 -0
  44. data/lib/mocha/is_a.rb +0 -2
  45. data/lib/mocha/logger.rb +0 -4
  46. data/lib/mocha/macos_version.rb +5 -0
  47. data/lib/mocha/method_matcher.rb +1 -5
  48. data/lib/mocha/minitest.rb +6 -0
  49. data/lib/mocha/mock.rb +105 -57
  50. data/lib/mocha/mockery.rb +70 -98
  51. data/lib/mocha/names.rb +2 -12
  52. data/lib/mocha/not_initialized_error.rb +7 -0
  53. data/lib/mocha/object_methods.rb +25 -31
  54. data/lib/mocha/parameter_matchers/all_of.rb +2 -8
  55. data/lib/mocha/parameter_matchers/any_of.rb +2 -8
  56. data/lib/mocha/parameter_matchers/any_parameters.rb +3 -9
  57. data/lib/mocha/parameter_matchers/anything.rb +2 -8
  58. data/lib/mocha/parameter_matchers/base.rb +7 -13
  59. data/lib/mocha/parameter_matchers/equals.rb +1 -7
  60. data/lib/mocha/parameter_matchers/{query_string.rb → equivalent_uri.rb} +14 -15
  61. data/lib/mocha/parameter_matchers/has_entries.rb +2 -7
  62. data/lib/mocha/parameter_matchers/has_entry.rb +26 -21
  63. data/lib/mocha/parameter_matchers/has_key.rb +2 -7
  64. data/lib/mocha/parameter_matchers/has_keys.rb +53 -0
  65. data/lib/mocha/parameter_matchers/has_value.rb +2 -7
  66. data/lib/mocha/parameter_matchers/includes.rb +50 -8
  67. data/lib/mocha/parameter_matchers/instance_methods.rb +27 -0
  68. data/lib/mocha/parameter_matchers/instance_of.rb +0 -6
  69. data/lib/mocha/parameter_matchers/is_a.rb +2 -7
  70. data/lib/mocha/parameter_matchers/kind_of.rb +2 -6
  71. data/lib/mocha/parameter_matchers/not.rb +2 -7
  72. data/lib/mocha/parameter_matchers/optionally.rb +4 -10
  73. data/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb +64 -0
  74. data/lib/mocha/parameter_matchers/regexp_matches.rb +0 -6
  75. data/lib/mocha/parameter_matchers/responds_with.rb +3 -8
  76. data/lib/mocha/parameter_matchers/yaml_equivalent.rb +2 -6
  77. data/lib/mocha/parameter_matchers.rb +3 -4
  78. data/lib/mocha/parameters_matcher.rb +8 -11
  79. data/lib/mocha/raised_exception.rb +11 -0
  80. data/lib/mocha/receivers.rb +10 -14
  81. data/lib/mocha/return_values.rb +4 -8
  82. data/lib/mocha/ruby_version.rb +3 -0
  83. data/lib/mocha/sequence.rb +4 -9
  84. data/lib/mocha/single_return_value.rb +2 -5
  85. data/lib/mocha/state_machine.rb +33 -46
  86. data/lib/mocha/stubbed_method.rb +88 -0
  87. data/lib/mocha/stubbing_error.rb +2 -13
  88. data/lib/mocha/test_unit.rb +5 -2
  89. data/lib/mocha/thrower.rb +4 -6
  90. data/lib/mocha/thrown_object.rb +12 -0
  91. data/lib/mocha/version.rb +1 -1
  92. data/lib/mocha/yield_parameters.rb +7 -17
  93. data/mocha.gemspec +16 -43
  94. data/yard-templates/default/layout/html/google_analytics.erb +6 -9
  95. data/yard-templates/default/layout/html/setup.rb +2 -3
  96. metadata +38 -246
  97. data/bin/build-matrix +0 -71
  98. data/gemfiles/Gemfile.minitest.1.3.0 +0 -7
  99. data/gemfiles/Gemfile.minitest.1.4.0 +0 -7
  100. data/gemfiles/Gemfile.minitest.1.4.1 +0 -7
  101. data/gemfiles/Gemfile.minitest.1.4.2 +0 -7
  102. data/gemfiles/Gemfile.minitest.2.0.0 +0 -7
  103. data/gemfiles/Gemfile.minitest.2.0.1 +0 -7
  104. data/gemfiles/Gemfile.minitest.2.11.0 +0 -7
  105. data/gemfiles/Gemfile.minitest.2.11.2 +0 -7
  106. data/gemfiles/Gemfile.minitest.2.3.0 +0 -7
  107. data/gemfiles/Gemfile.test-unit.2.0.0 +0 -7
  108. data/gemfiles/Gemfile.test-unit.2.0.1 +0 -7
  109. data/gemfiles/Gemfile.test-unit.2.0.3 +0 -7
  110. data/init.rb +0 -3
  111. data/lib/mocha/class_method.rb +0 -127
  112. data/lib/mocha/integration/mini_test/nothing.rb +0 -19
  113. data/lib/mocha/integration/mini_test/version_13.rb +0 -51
  114. data/lib/mocha/integration/mini_test/version_140.rb +0 -51
  115. data/lib/mocha/integration/mini_test/version_141.rb +0 -62
  116. data/lib/mocha/integration/mini_test/version_142_to_172.rb +0 -62
  117. data/lib/mocha/integration/mini_test/version_200.rb +0 -63
  118. data/lib/mocha/integration/mini_test/version_201_to_222.rb +0 -63
  119. data/lib/mocha/integration/mini_test/version_2110_to_2111.rb +0 -67
  120. data/lib/mocha/integration/mini_test/version_2112_to_320.rb +0 -70
  121. data/lib/mocha/integration/mini_test/version_230_to_2101.rb +0 -65
  122. data/lib/mocha/integration/test_unit/gem_version_200.rb +0 -59
  123. data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +0 -59
  124. data/lib/mocha/integration/test_unit/gem_version_203_to_220.rb +0 -59
  125. data/lib/mocha/integration/test_unit/gem_version_230_to_250.rb +0 -65
  126. data/lib/mocha/integration/test_unit/nothing.rb +0 -19
  127. data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +0 -58
  128. data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +0 -60
  129. data/lib/mocha/integration.rb +0 -14
  130. data/lib/mocha/mini_test.rb +0 -3
  131. data/lib/mocha/module_method.rb +0 -16
  132. data/lib/mocha/module_methods.rb +0 -14
  133. data/lib/mocha/multiple_yields.rb +0 -20
  134. data/lib/mocha/no_yields.rb +0 -11
  135. data/lib/mocha/parameter_matchers/object.rb +0 -17
  136. data/lib/mocha/pretty_parameters.rb +0 -28
  137. data/lib/mocha/setup.rb +0 -9
  138. data/lib/mocha/single_yield.rb +0 -18
  139. data/lib/mocha/standalone.rb +0 -4
  140. data/lib/mocha/unexpected_invocation.rb +0 -26
  141. data/lib/mocha_standalone.rb +0 -4
  142. data/test/acceptance/acceptance_test_helper.rb +0 -41
  143. data/test/acceptance/bug_18914_test.rb +0 -43
  144. data/test/acceptance/bug_21465_test.rb +0 -34
  145. data/test/acceptance/bug_21563_test.rb +0 -25
  146. data/test/acceptance/exception_rescue_test.rb +0 -55
  147. data/test/acceptance/expectations_on_multiple_methods_test.rb +0 -55
  148. data/test/acceptance/expected_invocation_count_test.rb +0 -232
  149. data/test/acceptance/failure_messages_test.rb +0 -64
  150. data/test/acceptance/issue_65_test.rb +0 -63
  151. data/test/acceptance/issue_70_test.rb +0 -55
  152. data/test/acceptance/mocha_example_test.rb +0 -98
  153. data/test/acceptance/mocha_test_result_test.rb +0 -84
  154. data/test/acceptance/mock_test.rb +0 -100
  155. data/test/acceptance/mock_with_initializer_block_test.rb +0 -51
  156. data/test/acceptance/mocked_methods_dispatch_test.rb +0 -78
  157. data/test/acceptance/multiple_expectations_failure_message_test.rb +0 -68
  158. data/test/acceptance/optional_parameters_test.rb +0 -70
  159. data/test/acceptance/parameter_matcher_test.rb +0 -337
  160. data/test/acceptance/partial_mocks_test.rb +0 -47
  161. data/test/acceptance/prepend_test.rb +0 -88
  162. data/test/acceptance/raise_exception_test.rb +0 -39
  163. data/test/acceptance/return_value_test.rb +0 -52
  164. data/test/acceptance/sequence_test.rb +0 -192
  165. data/test/acceptance/states_test.rb +0 -70
  166. data/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb +0 -34
  167. data/test/acceptance/stub_any_instance_method_test.rb +0 -238
  168. data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +0 -106
  169. data/test/acceptance/stub_class_method_defined_on_class_test.rb +0 -78
  170. data/test/acceptance/stub_class_method_defined_on_module_test.rb +0 -75
  171. data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +0 -112
  172. data/test/acceptance/stub_everything_test.rb +0 -56
  173. data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +0 -93
  174. data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +0 -69
  175. data/test/acceptance/stub_instance_method_defined_on_class_test.rb +0 -69
  176. data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +0 -75
  177. data/test/acceptance/stub_instance_method_defined_on_module_test.rb +0 -75
  178. data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +0 -75
  179. data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +0 -70
  180. data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +0 -72
  181. data/test/acceptance/stub_module_method_test.rb +0 -163
  182. data/test/acceptance/stub_test.rb +0 -52
  183. data/test/acceptance/stubba_example_test.rb +0 -102
  184. data/test/acceptance/stubba_test_result_test.rb +0 -66
  185. data/test/acceptance/stubbing_error_backtrace_test.rb +0 -64
  186. data/test/acceptance/stubbing_frozen_object_test.rb +0 -88
  187. data/test/acceptance/stubbing_method_accepting_block_parameter_test.rb +0 -48
  188. data/test/acceptance/stubbing_method_unnecessarily_test.rb +0 -65
  189. data/test/acceptance/stubbing_nil_test.rb +0 -59
  190. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +0 -130
  191. data/test/acceptance/stubbing_non_existent_class_method_test.rb +0 -157
  192. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +0 -147
  193. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +0 -130
  194. data/test/acceptance/stubbing_non_public_class_method_test.rb +0 -163
  195. data/test/acceptance/stubbing_non_public_instance_method_test.rb +0 -143
  196. data/test/acceptance/stubbing_on_non_mock_object_test.rb +0 -64
  197. data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +0 -35
  198. data/test/acceptance/throw_test.rb +0 -45
  199. data/test/acceptance/unexpected_invocation_test.rb +0 -25
  200. data/test/acceptance/unstubbing_test.rb +0 -168
  201. data/test/assertions.rb +0 -6
  202. data/test/deprecation_disabler.rb +0 -15
  203. data/test/execution_point.rb +0 -36
  204. data/test/integration/mini_test_test.rb +0 -8
  205. data/test/integration/shared_tests.rb +0 -174
  206. data/test/integration/test_unit_test.rb +0 -8
  207. data/test/method_definer.rb +0 -24
  208. data/test/mini_test_result.rb +0 -90
  209. data/test/minitest_result.rb +0 -49
  210. data/test/simple_counter.rb +0 -13
  211. data/test/test_helper.rb +0 -50
  212. data/test/test_runner.rb +0 -58
  213. data/test/test_unit_result.rb +0 -20
  214. data/test/unit/any_instance_method_test.rb +0 -132
  215. data/test/unit/array_inspect_test.rb +0 -16
  216. data/test/unit/backtrace_filter_test.rb +0 -19
  217. data/test/unit/cardinality_test.rb +0 -56
  218. data/test/unit/central_test.rb +0 -100
  219. data/test/unit/change_state_side_effect_test.rb +0 -41
  220. data/test/unit/class_method_test.rb +0 -223
  221. data/test/unit/class_methods_test.rb +0 -40
  222. data/test/unit/configuration_test.rb +0 -38
  223. data/test/unit/date_time_inspect_test.rb +0 -21
  224. data/test/unit/exception_raiser_test.rb +0 -42
  225. data/test/unit/expectation_list_test.rb +0 -82
  226. data/test/unit/expectation_test.rb +0 -497
  227. data/test/unit/hash_inspect_test.rb +0 -16
  228. data/test/unit/hooks_test.rb +0 -29
  229. data/test/unit/in_state_ordering_constraint_test.rb +0 -43
  230. data/test/unit/method_matcher_test.rb +0 -28
  231. data/test/unit/mock_test.rb +0 -341
  232. data/test/unit/mockery_test.rb +0 -151
  233. data/test/unit/module_methods_test.rb +0 -19
  234. data/test/unit/multiple_yields_test.rb +0 -18
  235. data/test/unit/no_yields_test.rb +0 -18
  236. data/test/unit/object_inspect_test.rb +0 -38
  237. data/test/unit/object_methods_test.rb +0 -46
  238. data/test/unit/parameter_matchers/all_of_test.rb +0 -26
  239. data/test/unit/parameter_matchers/any_of_test.rb +0 -26
  240. data/test/unit/parameter_matchers/anything_test.rb +0 -21
  241. data/test/unit/parameter_matchers/equals_test.rb +0 -25
  242. data/test/unit/parameter_matchers/has_entries_test.rb +0 -51
  243. data/test/unit/parameter_matchers/has_entry_test.rb +0 -129
  244. data/test/unit/parameter_matchers/has_key_test.rb +0 -55
  245. data/test/unit/parameter_matchers/has_value_test.rb +0 -57
  246. data/test/unit/parameter_matchers/includes_test.rb +0 -59
  247. data/test/unit/parameter_matchers/instance_of_test.rb +0 -25
  248. data/test/unit/parameter_matchers/is_a_test.rb +0 -25
  249. data/test/unit/parameter_matchers/kind_of_test.rb +0 -25
  250. data/test/unit/parameter_matchers/not_test.rb +0 -26
  251. data/test/unit/parameter_matchers/regexp_matches_test.rb +0 -46
  252. data/test/unit/parameter_matchers/responds_with_test.rb +0 -32
  253. data/test/unit/parameter_matchers/stub_matcher.rb +0 -27
  254. data/test/unit/parameter_matchers/yaml_equivalent_test.rb +0 -25
  255. data/test/unit/parameters_matcher_test.rb +0 -121
  256. data/test/unit/receivers_test.rb +0 -66
  257. data/test/unit/return_values_test.rb +0 -63
  258. data/test/unit/sequence_test.rb +0 -104
  259. data/test/unit/single_return_value_test.rb +0 -14
  260. data/test/unit/single_yield_test.rb +0 -18
  261. data/test/unit/state_machine_test.rb +0 -98
  262. data/test/unit/string_inspect_test.rb +0 -11
  263. data/test/unit/thrower_test.rb +0 -20
  264. data/test/unit/yield_parameters_test.rb +0 -93
@@ -1,75 +0,0 @@
1
- require File.expand_path('../acceptance_test_helper', __FILE__)
2
- require 'mocha/setup'
3
-
4
- class StubInstanceMethodDefinedOnObjectClassTest < Mocha::TestCase
5
-
6
- include AcceptanceTest
7
-
8
- def setup
9
- setup_acceptance_test
10
- end
11
-
12
- def teardown
13
- teardown_acceptance_test
14
- end
15
-
16
- def test_should_stub_public_method_and_leave_it_unchanged_after_test
17
- Object.class_eval do
18
- def my_instance_method
19
- :original_return_value
20
- end
21
- public :my_instance_method
22
- end
23
- instance = Class.new.new
24
- assert_snapshot_unchanged(instance) do
25
- test_result = run_as_test do
26
- instance.stubs(:my_instance_method).returns(:new_return_value)
27
- assert_equal :new_return_value, instance.my_instance_method
28
- end
29
- assert_passed(test_result)
30
- end
31
- assert_equal :original_return_value, instance.my_instance_method
32
- ensure
33
- Object.class_eval { remove_method :my_instance_method }
34
- end
35
-
36
- def test_should_stub_protected_method_and_leave_it_unchanged_after_test
37
- Object.class_eval do
38
- def my_instance_method
39
- :original_return_value
40
- end
41
- protected :my_instance_method
42
- end
43
- instance = Class.new.new
44
- assert_snapshot_unchanged(instance) do
45
- test_result = run_as_test do
46
- instance.stubs(:my_instance_method).returns(:new_return_value)
47
- assert_equal :new_return_value, instance.send(:my_instance_method)
48
- end
49
- assert_passed(test_result)
50
- end
51
- assert_equal :original_return_value, instance.send(:my_instance_method)
52
- ensure
53
- Object.class_eval { remove_method :my_instance_method }
54
- end
55
-
56
- def test_should_stub_private_method_and_leave_it_unchanged_after_test
57
- Object.class_eval do
58
- def my_instance_method
59
- :original_return_value
60
- end
61
- private :my_instance_method
62
- end
63
- instance = Class.new.new
64
- assert_snapshot_unchanged(instance) do
65
- test_result = run_as_test do
66
- instance.stubs(:my_instance_method).returns(:new_return_value)
67
- assert_equal :new_return_value, instance.send(:my_instance_method)
68
- end
69
- assert_passed(test_result)
70
- end
71
- assert_equal :original_return_value, instance.send(:my_instance_method)
72
- ensure
73
- Object.class_eval { remove_method :my_instance_method }
74
- end
75
- end
@@ -1,70 +0,0 @@
1
- require File.expand_path('../acceptance_test_helper', __FILE__)
2
- require 'mocha/setup'
3
-
4
- class StubInstanceMethodDefinedOnSingletonClassTest < Mocha::TestCase
5
-
6
- include AcceptanceTest
7
-
8
- def setup
9
- setup_acceptance_test
10
- end
11
-
12
- def teardown
13
- teardown_acceptance_test
14
- end
15
-
16
- def test_should_stub_public_method_and_leave_it_unchanged_after_test
17
- instance = Class.new.new
18
- class << instance
19
- def my_singleton_method
20
- :original_return_value
21
- end
22
- public :my_singleton_method
23
- end
24
- assert_snapshot_unchanged(instance) do
25
- test_result = run_as_test do
26
- instance.stubs(:my_singleton_method).returns(:stubbed_return_value)
27
- assert_equal :stubbed_return_value, instance.my_singleton_method
28
- end
29
- assert_passed(test_result)
30
- end
31
- assert_equal :original_return_value, instance.my_singleton_method
32
- end
33
-
34
- def test_should_stub_protected_method_and_leave_it_unchanged_after_test
35
- instance = Class.new.new
36
- class << instance
37
- def my_singleton_method
38
- :original_return_value
39
- end
40
- protected :my_singleton_method
41
- end
42
- assert_snapshot_unchanged(instance) do
43
- test_result = run_as_test do
44
- instance.stubs(:my_singleton_method).returns(:stubbed_return_value)
45
- assert_equal :stubbed_return_value, instance.send(:my_singleton_method)
46
- end
47
- assert_passed(test_result)
48
- end
49
- assert_equal :original_return_value, instance.send(:my_singleton_method)
50
- end
51
-
52
- def test_should_stub_private_method_and_leave_it_unchanged_after_test
53
- instance = Class.new.new
54
- class << instance
55
- def my_singleton_method
56
- :original_return_value
57
- end
58
- private :my_singleton_method
59
- end
60
- assert_snapshot_unchanged(instance) do
61
- test_result = run_as_test do
62
- instance.stubs(:my_singleton_method).returns(:stubbed_return_value)
63
- assert_equal :stubbed_return_value, instance.send(:my_singleton_method)
64
- end
65
- assert_passed(test_result)
66
- end
67
- assert_equal :original_return_value, instance.send(:my_singleton_method)
68
- end
69
-
70
- end
@@ -1,72 +0,0 @@
1
- require File.expand_path('../acceptance_test_helper', __FILE__)
2
- require 'mocha/setup'
3
-
4
- class StubInstanceMethodDefinedOnSuperclassTest < Mocha::TestCase
5
-
6
- include AcceptanceTest
7
-
8
- def setup
9
- setup_acceptance_test
10
- end
11
-
12
- def teardown
13
- teardown_acceptance_test
14
- end
15
-
16
- def test_should_stub_public_method_and_leave_it_unchanged_after_test
17
- superklass = Class.new do
18
- def my_superclass_method
19
- :original_return_value
20
- end
21
- public :my_superclass_method
22
- end
23
- klass = Class.new(superklass)
24
- instance = klass.new
25
- assert_snapshot_unchanged(instance) do
26
- test_result = run_as_test do
27
- instance.stubs(:my_superclass_method).returns(:new_return_value)
28
- assert_equal :new_return_value, instance.my_superclass_method
29
- end
30
- assert_passed(test_result)
31
- end
32
- assert_equal :original_return_value, instance.my_superclass_method
33
- end
34
-
35
- def test_should_stub_protected_method_and_leave_it_unchanged_after_test
36
- superklass = Class.new do
37
- def my_superclass_method
38
- :original_return_value
39
- end
40
- protected :my_superclass_method
41
- end
42
- klass = Class.new(superklass)
43
- instance = klass.new
44
- assert_snapshot_unchanged(instance) do
45
- test_result = run_as_test do
46
- instance.stubs(:my_superclass_method).returns(:new_return_value)
47
- assert_equal :new_return_value, instance.send(:my_superclass_method)
48
- end
49
- assert_passed(test_result)
50
- end
51
- assert_equal :original_return_value, instance.send(:my_superclass_method)
52
- end
53
-
54
- def test_should_stub_private_method_and_leave_it_unchanged_after_test
55
- superklass = Class.new do
56
- def my_superclass_method
57
- :original_return_value
58
- end
59
- private :my_superclass_method
60
- end
61
- klass = Class.new(superklass)
62
- instance = klass.new
63
- assert_snapshot_unchanged(instance) do
64
- test_result = run_as_test do
65
- instance.stubs(:my_superclass_method).returns(:new_return_value)
66
- assert_equal :new_return_value, instance.send(:my_superclass_method)
67
- end
68
- assert_passed(test_result)
69
- end
70
- assert_equal :original_return_value, instance.send(:my_superclass_method)
71
- end
72
- end
@@ -1,163 +0,0 @@
1
- require File.expand_path('../acceptance_test_helper', __FILE__)
2
- require 'mocha/setup'
3
-
4
- class StubModuleMethodTest < Mocha::TestCase
5
-
6
- include AcceptanceTest
7
-
8
- def setup
9
- setup_acceptance_test
10
- end
11
-
12
- def teardown
13
- teardown_acceptance_test
14
- end
15
-
16
- def test_should_stub_method_within_test
17
- mod = Module.new { def self.my_module_method; :original_return_value; end }
18
- test_result = run_as_test do
19
- mod.stubs(:my_module_method).returns(:new_return_value)
20
- assert_equal :new_return_value, mod.my_module_method
21
- end
22
- assert_passed(test_result)
23
- end
24
-
25
- def test_should_leave_stubbed_public_method_unchanged_after_test
26
- mod = Module.new { class << self; def my_module_method; :original_return_value; end; public :my_module_method; end }
27
- run_as_test do
28
- mod.stubs(:my_module_method).returns(:new_return_value)
29
- end
30
- assert mod.public_methods(false).any? { |m| m.to_s == 'my_module_method' }
31
- assert_equal :original_return_value, mod.my_module_method
32
- end
33
-
34
- def test_should_leave_stubbed_protected_method_unchanged_after_test
35
- mod = Module.new { class << self; def my_module_method; :original_return_value; end; protected :my_module_method; def my_unprotected_module_method; my_module_method; end; end }
36
- run_as_test do
37
- mod.stubs(:my_module_method).returns(:new_return_value)
38
- end
39
- assert mod.protected_methods(false).any? { |m| m.to_s == 'my_module_method' }
40
- assert_equal :original_return_value, mod.my_unprotected_module_method
41
- end
42
-
43
- def test_should_leave_stubbed_private_method_unchanged_after_test
44
- mod = Module.new { class << self; def my_module_method; :original_return_value; end; private :my_module_method; end }
45
- run_as_test do
46
- mod.stubs(:my_module_method).returns(:new_return_value)
47
- end
48
- assert mod.private_methods(false).any? { |m| m.to_s == 'my_module_method' }
49
- assert_equal :original_return_value, mod.send(:my_module_method)
50
- end
51
-
52
- def test_should_reset_expectations_after_test
53
- mod = Module.new { def self.my_module_method; :original_return_value; end }
54
- run_as_test do
55
- mod.stubs(:my_module_method)
56
- end
57
- assert_equal 0, mod.mocha.__expectations__.length
58
- end
59
-
60
- def test_should_be_able_to_stub_a_superclass_method
61
- supermod = Module.new { def self.my_superclass_method; :original_return_value; end }
62
- mod = Module.new { include supermod }
63
- test_result = run_as_test do
64
- mod.stubs(:my_superclass_method).returns(:new_return_value)
65
- assert_equal :new_return_value, mod.my_superclass_method
66
- end
67
- assert_passed(test_result)
68
- assert supermod.public_methods.any? { |m| m.to_s == 'my_superclass_method' }
69
- assert !mod.public_methods(false).any? { |m| m.to_s == 'my_superclass_method' }
70
- assert_equal :original_return_value, supermod.my_superclass_method
71
- end
72
-
73
- def test_should_be_able_to_stub_method_if_ruby18_public_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
74
- ruby18_mod = Module.new do
75
- class << self
76
- def public_methods(include_superclass = true)
77
- ['my_module_method']
78
- end
79
- end
80
- end
81
- test_result = run_as_test do
82
- ruby18_mod.stubs(:my_module_method).returns(:new_return_value)
83
- assert_equal :new_return_value, ruby18_mod.my_module_method
84
- end
85
- assert_passed(test_result)
86
- end
87
-
88
- def test_should_be_able_to_stub_method_if_ruby19_public_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
89
- ruby19_mod = Module.new do
90
- class << self
91
- def public_methods(include_superclass = true)
92
- [:my_module_method]
93
- end
94
- end
95
- end
96
- test_result = run_as_test do
97
- ruby19_mod.stubs(:my_module_method).returns(:new_return_value)
98
- assert_equal :new_return_value, ruby19_mod.my_module_method
99
- end
100
- assert_passed(test_result)
101
- end
102
-
103
- def test_should_be_able_to_stub_method_if_ruby_18_protected_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
104
- ruby18_mod = Module.new do
105
- class << self
106
- def protected_methods(include_superclass = true)
107
- ['my_module_method']
108
- end
109
- end
110
- end
111
- test_result = run_as_test do
112
- ruby18_mod.stubs(:my_module_method).returns(:new_return_value)
113
- assert_equal :new_return_value, ruby18_mod.my_module_method
114
- end
115
- assert_passed(test_result)
116
- end
117
-
118
- def test_should_be_able_to_stub_method_if_ruby19_protected_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
119
- ruby19_mod = Module.new do
120
- class << self
121
- def protected_methods(include_superclass = true)
122
- [:my_module_method]
123
- end
124
- end
125
- end
126
- test_result = run_as_test do
127
- ruby19_mod.stubs(:my_module_method).returns(:new_return_value)
128
- assert_equal :new_return_value, ruby19_mod.my_module_method
129
- end
130
- assert_passed(test_result)
131
- end
132
-
133
- def test_should_be_able_to_stub_method_if_ruby18_private_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
134
- ruby18_mod = Module.new do
135
- class << self
136
- def private_methods(include_superclass = true)
137
- ['my_module_method']
138
- end
139
- end
140
- end
141
- test_result = run_as_test do
142
- ruby18_mod.stubs(:my_module_method).returns(:new_return_value)
143
- assert_equal :new_return_value, ruby18_mod.my_module_method
144
- end
145
- assert_passed(test_result)
146
- end
147
-
148
- def test_should_be_able_to_stub_method_if_ruby19_private_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
149
- ruby19_mod = Module.new do
150
- class << self
151
- def private_methods(include_superclass = true)
152
- [:my_module_method]
153
- end
154
- end
155
- end
156
- test_result = run_as_test do
157
- ruby19_mod.stubs(:my_module_method).returns(:new_return_value)
158
- assert_equal :new_return_value, ruby19_mod.my_module_method
159
- end
160
- assert_passed(test_result)
161
- end
162
-
163
- end
@@ -1,52 +0,0 @@
1
- require File.expand_path('../acceptance_test_helper', __FILE__)
2
- require 'mocha/setup'
3
-
4
- class StubTest < Mocha::TestCase
5
-
6
- include AcceptanceTest
7
-
8
- def setup
9
- setup_acceptance_test
10
- end
11
-
12
- def teardown
13
- teardown_acceptance_test
14
- end
15
-
16
- def test_should_build_stub_and_explicitly_add_an_expectation
17
- test_result = run_as_test do
18
- foo = stub()
19
- foo.stubs(:bar)
20
- foo.bar
21
- end
22
- assert_passed(test_result)
23
- end
24
-
25
- def test_should_build_named_stub_and_explicitly_add_an_expectation
26
- test_result = run_as_test do
27
- foo = stub('foo')
28
- foo.stubs(:bar)
29
- foo.bar
30
- end
31
- assert_passed(test_result)
32
- end
33
-
34
- def test_should_build_stub_incorporating_two_expectations
35
- test_result = run_as_test do
36
- foo = stub(:bar => 'bar', :baz => 'baz')
37
- foo.bar
38
- foo.baz
39
- end
40
- assert_passed(test_result)
41
- end
42
-
43
- def test_should_build_named_stub_incorporating_two_expectations
44
- test_result = run_as_test do
45
- foo = stub('foo', :bar => 'bar', :baz => 'baz')
46
- foo.bar
47
- foo.baz
48
- end
49
- assert_passed(test_result)
50
- end
51
-
52
- end
@@ -1,102 +0,0 @@
1
- require File.expand_path('../../test_helper', __FILE__)
2
- require 'mocha/setup'
3
-
4
- class Widget
5
-
6
- def model
7
- 'original_model'
8
- end
9
-
10
- class << self
11
-
12
- def find(options)
13
- []
14
- end
15
-
16
- def create(attributes)
17
- Widget.new
18
- end
19
-
20
- end
21
-
22
- end
23
-
24
- module Thingy
25
-
26
- def self.wotsit
27
- :hoojamaflip
28
- end
29
-
30
- end
31
-
32
- class StubbaExampleTest < Mocha::TestCase
33
-
34
- def test_should_stub_instance_method
35
- widget = Widget.new
36
- widget.expects(:model).returns('different_model')
37
- assert_equal 'different_model', widget.model
38
- end
39
-
40
- def test_should_stub_module_method
41
- should_stub_module_method
42
- end
43
-
44
- def test_should_stub_module_method_again
45
- should_stub_module_method
46
- end
47
-
48
- def test_should_stub_class_method
49
- should_stub_class_method
50
- end
51
-
52
- def test_should_stub_class_method_again
53
- should_stub_class_method
54
- end
55
-
56
- def test_should_stub_instance_method_on_any_instance_of_a_class
57
- should_stub_instance_method_on_any_instance_of_a_class
58
- end
59
-
60
- def test_should_stub_instance_method_on_any_instance_of_a_class_again
61
- should_stub_instance_method_on_any_instance_of_a_class
62
- end
63
-
64
- def test_should_stub_two_different_class_methods
65
- should_stub_two_different_class_methods
66
- end
67
-
68
- def test_should_stub_two_different_class_methods_again
69
- should_stub_two_different_class_methods
70
- end
71
-
72
- private
73
-
74
- def should_stub_module_method
75
- Thingy.expects(:wotsit).returns(:dooda)
76
- assert_equal :dooda, Thingy.wotsit
77
- end
78
-
79
- def should_stub_class_method
80
- widgets = [Widget.new]
81
- Widget.expects(:find).with(:all).returns(widgets)
82
- assert_equal widgets, Widget.find(:all)
83
- end
84
-
85
- def should_stub_two_different_class_methods
86
- found_widgets = [Widget.new]
87
- created_widget = Widget.new
88
- Widget.expects(:find).with(:all).returns(found_widgets)
89
- Widget.expects(:create).with(:model => 'wombat').returns(created_widget)
90
- assert_equal found_widgets, Widget.find(:all)
91
- assert_equal created_widget, Widget.create(:model => 'wombat')
92
- end
93
-
94
- def should_stub_instance_method_on_any_instance_of_a_class
95
- Widget.any_instance.expects(:model).at_least_once.returns('another_model')
96
- widget_1 = Widget.new
97
- widget_2 = Widget.new
98
- assert_equal 'another_model', widget_1.model
99
- assert_equal 'another_model', widget_2.model
100
- end
101
-
102
- end
@@ -1,66 +0,0 @@
1
- require File.expand_path('../acceptance_test_helper', __FILE__)
2
- require 'mocha/setup'
3
- require 'execution_point'
4
-
5
- class StubbaTestResultTest < Mocha::TestCase
6
-
7
- include AcceptanceTest
8
-
9
- def setup
10
- setup_acceptance_test
11
- end
12
-
13
- def teardown
14
- teardown_acceptance_test
15
- end
16
-
17
- def test_should_include_expectation_verification_in_assertion_count
18
- test_result = run_as_test do
19
- object = Class.new { def message; end }.new
20
- object.expects(:message)
21
- object.message
22
- end
23
- assert_equal 1, test_result.assertion_count
24
- end
25
-
26
- def test_should_include_assertions_in_assertion_count
27
- test_result = run_as_test do
28
- assert true
29
- end
30
- assert_equal 1, test_result.assertion_count
31
- end
32
-
33
- def test_should_not_include_stubbing_expectation_verification_in_assertion_count
34
- test_result = run_as_test do
35
- object = Class.new { def message; end }.new
36
- object.stubs(:message)
37
- object.message
38
- end
39
- assert_equal 0, test_result.assertion_count
40
- end
41
-
42
- def test_should_include_expectation_verification_failure_in_failure_count
43
- test_result = run_as_test do
44
- object = Class.new { def message; end }.new
45
- object.expects(:message)
46
- end
47
- assert_equal 1, test_result.failure_count
48
- end
49
-
50
- def test_should_include_assertion_failure_in_failure_count
51
- test_result = run_as_test do
52
- flunk
53
- end
54
- assert_equal 1, test_result.failure_count
55
- end
56
-
57
- def test_should_display_backtrace_indicating_line_number_where_failing_assertion_was_called
58
- execution_point = nil
59
- test_result = run_as_test do
60
- execution_point = ExecutionPoint.current; flunk
61
- end
62
- assert_equal 1, test_result.failure_count
63
- assert_equal execution_point, ExecutionPoint.new(test_result.failures[0].location)
64
- end
65
-
66
- end
@@ -1,64 +0,0 @@
1
- require File.expand_path('../acceptance_test_helper', __FILE__)
2
- require 'mocha/setup'
3
- require 'execution_point'
4
-
5
- class StubbingErrorBacktraceTest < Mocha::TestCase
6
-
7
- include AcceptanceTest
8
-
9
- def setup
10
- setup_acceptance_test
11
- end
12
-
13
- def teardown
14
- teardown_acceptance_test
15
- end
16
-
17
- def test_should_display_backtrace_indicating_line_number_where_attempt_to_stub_non_existent_method_was_made
18
- execution_point = nil
19
- object = Object.new
20
- Mocha::Configuration.prevent(:stubbing_non_existent_method)
21
- test_result = run_as_test do
22
- execution_point = ExecutionPoint.current; object.stubs(:non_existent_method)
23
- end
24
- assert_equal 1, test_result.error_count
25
- assert_equal execution_point, ExecutionPoint.new(test_result.errors[0].exception.backtrace)
26
- end
27
-
28
- def test_should_display_backtrace_indicating_line_number_where_attempt_to_stub_non_public_method_was_made
29
- execution_point = nil
30
- object = Class.new do
31
- def non_public_method; end
32
- private :non_public_method
33
- end.new
34
- Mocha::Configuration.prevent(:stubbing_non_public_method)
35
- test_result = run_as_test do
36
- execution_point = ExecutionPoint.current; object.stubs(:non_public_method)
37
- end
38
- assert_equal 1, test_result.error_count
39
- assert_equal execution_point, ExecutionPoint.new(test_result.errors[0].exception.backtrace)
40
- end
41
-
42
- def test_should_display_backtrace_indicating_line_number_where_attempt_to_stub_method_on_non_mock_object_was_made
43
- execution_point = nil
44
- object = Object.new
45
- Mocha::Configuration.prevent(:stubbing_method_on_non_mock_object)
46
- test_result = run_as_test do
47
- execution_point = ExecutionPoint.current; object.stubs(:any_method)
48
- end
49
- assert_equal 1, test_result.error_count
50
- assert_equal execution_point, ExecutionPoint.new(test_result.errors[0].exception.backtrace)
51
- end
52
-
53
- def test_should_display_backtrace_indicating_line_number_where_method_was_unnecessarily_stubbed
54
- execution_point = nil
55
- object = Object.new
56
- Mocha::Configuration.prevent(:stubbing_method_unnecessarily)
57
- test_result = run_as_test do
58
- execution_point = ExecutionPoint.current; object.stubs(:unused_method)
59
- end
60
- assert_equal 1, test_result.error_count
61
- assert_equal execution_point, ExecutionPoint.new(test_result.errors[0].exception.backtrace)
62
- end
63
-
64
- end