mocha 0.5.6 → 3.1.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 (191) hide show
  1. checksums.yaml +7 -0
  2. data/.gemtest +0 -0
  3. data/.github/FUNDING.yml +1 -0
  4. data/.rubocop.yml +92 -0
  5. data/.rubocop_todo.yml +39 -0
  6. data/.yardopts +25 -0
  7. data/CONTRIBUTING.md +7 -0
  8. data/COPYING.md +3 -0
  9. data/Gemfile +17 -0
  10. data/{MIT-LICENSE → MIT-LICENSE.md} +2 -2
  11. data/README.md +361 -0
  12. data/RELEASE.md +1247 -0
  13. data/Rakefile +165 -123
  14. data/gemfiles/Gemfile.minitest.latest +8 -0
  15. data/gemfiles/Gemfile.rubocop +9 -0
  16. data/gemfiles/Gemfile.test-unit.latest +8 -0
  17. data/lib/mocha/any_instance_method.rb +12 -26
  18. data/lib/mocha/any_instance_receiver.rb +20 -0
  19. data/lib/mocha/api.rb +213 -0
  20. data/lib/mocha/argument_iterator.rb +17 -0
  21. data/lib/mocha/backtrace_filter.rb +25 -0
  22. data/lib/mocha/block_matchers.rb +33 -0
  23. data/lib/mocha/cardinality.rb +110 -0
  24. data/lib/mocha/central.rb +33 -22
  25. data/lib/mocha/change_state_side_effect.rb +17 -0
  26. data/lib/mocha/class_methods.rb +67 -0
  27. data/lib/mocha/configuration.rb +338 -0
  28. data/lib/mocha/default_name.rb +15 -0
  29. data/lib/mocha/default_receiver.rb +13 -0
  30. data/lib/mocha/deprecation.rb +6 -15
  31. data/lib/mocha/detection/minitest.rb +25 -0
  32. data/lib/mocha/detection/test_unit.rb +30 -0
  33. data/lib/mocha/error_with_filtered_backtrace.rb +15 -0
  34. data/lib/mocha/exception_raiser.rb +11 -10
  35. data/lib/mocha/expectation.rb +562 -171
  36. data/lib/mocha/expectation_error.rb +9 -14
  37. data/lib/mocha/expectation_error_factory.rb +37 -0
  38. data/lib/mocha/expectation_list.rb +30 -14
  39. data/lib/mocha/hooks.rb +55 -0
  40. data/lib/mocha/ignoring_warning.rb +20 -0
  41. data/lib/mocha/impersonating_any_instance_name.rb +13 -0
  42. data/lib/mocha/impersonating_name.rb +13 -0
  43. data/lib/mocha/in_state_ordering_constraint.rb +17 -0
  44. data/lib/mocha/inspect.rb +54 -30
  45. data/lib/mocha/instance_method.rb +17 -4
  46. data/lib/mocha/integration/assertion_counter.rb +15 -0
  47. data/lib/mocha/integration/minitest/adapter.rb +71 -0
  48. data/lib/mocha/integration/minitest.rb +29 -0
  49. data/lib/mocha/integration/monkey_patcher.rb +26 -0
  50. data/lib/mocha/integration/test_unit/adapter.rb +61 -0
  51. data/lib/mocha/integration/test_unit.rb +29 -0
  52. data/lib/mocha/integration.rb +5 -0
  53. data/lib/mocha/invocation.rb +76 -0
  54. data/lib/mocha/logger.rb +26 -0
  55. data/lib/mocha/macos_version.rb +7 -0
  56. data/lib/mocha/method_matcher.rb +8 -10
  57. data/lib/mocha/minitest.rb +7 -0
  58. data/lib/mocha/mock.rb +333 -108
  59. data/lib/mocha/mockery.rb +192 -0
  60. data/lib/mocha/name.rb +13 -0
  61. data/lib/mocha/not_initialized_error.rb +9 -0
  62. data/lib/mocha/object_methods.rb +183 -0
  63. data/lib/mocha/object_receiver.rb +20 -0
  64. data/lib/mocha/parameter_matchers/all_of.rb +38 -28
  65. data/lib/mocha/parameter_matchers/any_of.rb +44 -33
  66. data/lib/mocha/parameter_matchers/any_parameters.rb +33 -26
  67. data/lib/mocha/parameter_matchers/anything.rb +31 -22
  68. data/lib/mocha/parameter_matchers/base_methods.rb +64 -0
  69. data/lib/mocha/parameter_matchers/equals.rb +36 -25
  70. data/lib/mocha/parameter_matchers/equivalent_uri.rb +65 -0
  71. data/lib/mocha/parameter_matchers/has_entries.rb +48 -29
  72. data/lib/mocha/parameter_matchers/has_entry.rb +90 -42
  73. data/lib/mocha/parameter_matchers/has_key.rb +39 -26
  74. data/lib/mocha/parameter_matchers/has_keys.rb +59 -0
  75. data/lib/mocha/parameter_matchers/has_value.rb +39 -26
  76. data/lib/mocha/parameter_matchers/includes.rb +88 -23
  77. data/lib/mocha/parameter_matchers/instance_methods.rb +28 -0
  78. data/lib/mocha/parameter_matchers/instance_of.rb +37 -26
  79. data/lib/mocha/parameter_matchers/is_a.rb +38 -26
  80. data/lib/mocha/parameter_matchers/kind_of.rb +39 -26
  81. data/lib/mocha/parameter_matchers/not.rb +37 -26
  82. data/lib/mocha/parameter_matchers/optionally.rb +52 -17
  83. data/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb +91 -0
  84. data/lib/mocha/parameter_matchers/regexp_matches.rb +37 -25
  85. data/lib/mocha/parameter_matchers/responds_with.rb +82 -0
  86. data/lib/mocha/parameter_matchers/yaml_equivalent.rb +55 -0
  87. data/lib/mocha/parameter_matchers.rb +12 -5
  88. data/lib/mocha/parameters_matcher.rb +28 -19
  89. data/lib/mocha/raised_exception.rb +13 -0
  90. data/lib/mocha/return_values.rb +13 -18
  91. data/lib/mocha/ruby_version.rb +7 -0
  92. data/lib/mocha/sequence.rb +23 -17
  93. data/lib/mocha/single_return_value.rb +8 -18
  94. data/lib/mocha/state_machine.rb +95 -0
  95. data/lib/mocha/stubbed_method.rb +96 -0
  96. data/lib/mocha/stubbing_error.rb +10 -0
  97. data/lib/mocha/test_unit.rb +7 -0
  98. data/lib/mocha/thrower.rb +15 -0
  99. data/lib/mocha/thrown_object.rb +14 -0
  100. data/lib/mocha/version.rb +5 -0
  101. data/lib/mocha/yield_parameters.rb +12 -20
  102. data/lib/mocha.rb +19 -17
  103. data/mocha.gemspec +40 -0
  104. metadata +129 -145
  105. data/COPYING +0 -3
  106. data/README +0 -35
  107. data/RELEASE +0 -188
  108. data/examples/misc.rb +0 -44
  109. data/examples/mocha.rb +0 -26
  110. data/examples/stubba.rb +0 -65
  111. data/lib/mocha/auto_verify.rb +0 -118
  112. data/lib/mocha/class_method.rb +0 -66
  113. data/lib/mocha/infinite_range.rb +0 -25
  114. data/lib/mocha/is_a.rb +0 -9
  115. data/lib/mocha/metaclass.rb +0 -7
  116. data/lib/mocha/missing_expectation.rb +0 -17
  117. data/lib/mocha/multiple_yields.rb +0 -20
  118. data/lib/mocha/no_yields.rb +0 -11
  119. data/lib/mocha/object.rb +0 -110
  120. data/lib/mocha/parameter_matchers/base.rb +0 -15
  121. data/lib/mocha/parameter_matchers/object.rb +0 -9
  122. data/lib/mocha/pretty_parameters.rb +0 -28
  123. data/lib/mocha/setup_and_teardown.rb +0 -23
  124. data/lib/mocha/single_yield.rb +0 -18
  125. data/lib/mocha/standalone.rb +0 -32
  126. data/lib/mocha/stub.rb +0 -18
  127. data/lib/mocha/test_case_adapter.rb +0 -49
  128. data/lib/mocha_standalone.rb +0 -2
  129. data/lib/stubba.rb +0 -2
  130. data/test/acceptance/expected_invocation_count_acceptance_test.rb +0 -187
  131. data/test/acceptance/mocha_acceptance_test.rb +0 -98
  132. data/test/acceptance/mock_with_initializer_block_acceptance_test.rb +0 -44
  133. data/test/acceptance/mocked_methods_dispatch_acceptance_test.rb +0 -71
  134. data/test/acceptance/optional_parameters_acceptance_test.rb +0 -63
  135. data/test/acceptance/parameter_matcher_acceptance_test.rb +0 -117
  136. data/test/acceptance/partial_mocks_acceptance_test.rb +0 -40
  137. data/test/acceptance/sequence_acceptance_test.rb +0 -179
  138. data/test/acceptance/standalone_acceptance_test.rb +0 -131
  139. data/test/acceptance/stubba_acceptance_test.rb +0 -102
  140. data/test/active_record_test_case.rb +0 -36
  141. data/test/deprecation_disabler.rb +0 -15
  142. data/test/execution_point.rb +0 -34
  143. data/test/integration/mocha_test_result_integration_test.rb +0 -105
  144. data/test/integration/stubba_integration_test.rb +0 -89
  145. data/test/integration/stubba_test_result_integration_test.rb +0 -85
  146. data/test/method_definer.rb +0 -18
  147. data/test/test_helper.rb +0 -12
  148. data/test/test_runner.rb +0 -31
  149. data/test/unit/any_instance_method_test.rb +0 -126
  150. data/test/unit/array_inspect_test.rb +0 -16
  151. data/test/unit/auto_verify_test.rb +0 -129
  152. data/test/unit/central_test.rb +0 -124
  153. data/test/unit/class_method_test.rb +0 -200
  154. data/test/unit/date_time_inspect_test.rb +0 -21
  155. data/test/unit/expectation_error_test.rb +0 -24
  156. data/test/unit/expectation_list_test.rb +0 -75
  157. data/test/unit/expectation_raiser_test.rb +0 -28
  158. data/test/unit/expectation_test.rb +0 -483
  159. data/test/unit/hash_inspect_test.rb +0 -16
  160. data/test/unit/infinite_range_test.rb +0 -53
  161. data/test/unit/metaclass_test.rb +0 -22
  162. data/test/unit/method_matcher_test.rb +0 -23
  163. data/test/unit/missing_expectation_test.rb +0 -42
  164. data/test/unit/mock_test.rb +0 -323
  165. data/test/unit/multiple_yields_test.rb +0 -18
  166. data/test/unit/no_yield_test.rb +0 -18
  167. data/test/unit/object_inspect_test.rb +0 -37
  168. data/test/unit/object_test.rb +0 -165
  169. data/test/unit/parameter_matchers/all_of_test.rb +0 -26
  170. data/test/unit/parameter_matchers/any_of_test.rb +0 -26
  171. data/test/unit/parameter_matchers/anything_test.rb +0 -21
  172. data/test/unit/parameter_matchers/has_entries_test.rb +0 -30
  173. data/test/unit/parameter_matchers/has_entry_test.rb +0 -40
  174. data/test/unit/parameter_matchers/has_key_test.rb +0 -25
  175. data/test/unit/parameter_matchers/has_value_test.rb +0 -25
  176. data/test/unit/parameter_matchers/includes_test.rb +0 -25
  177. data/test/unit/parameter_matchers/instance_of_test.rb +0 -25
  178. data/test/unit/parameter_matchers/is_a_test.rb +0 -25
  179. data/test/unit/parameter_matchers/kind_of_test.rb +0 -25
  180. data/test/unit/parameter_matchers/not_test.rb +0 -26
  181. data/test/unit/parameter_matchers/regexp_matches_test.rb +0 -25
  182. data/test/unit/parameter_matchers/stub_matcher.rb +0 -23
  183. data/test/unit/parameters_matcher_test.rb +0 -121
  184. data/test/unit/return_values_test.rb +0 -63
  185. data/test/unit/sequence_test.rb +0 -104
  186. data/test/unit/setup_and_teardown_test.rb +0 -76
  187. data/test/unit/single_return_value_test.rb +0 -33
  188. data/test/unit/single_yield_test.rb +0 -18
  189. data/test/unit/string_inspect_test.rb +0 -11
  190. data/test/unit/stub_test.rb +0 -24
  191. data/test/unit/yield_parameters_test.rb +0 -93
data/RELEASE DELETED
@@ -1,188 +0,0 @@
1
- = 0.5.5 (r167)
2
-
3
- - Renamed Matches parameter matcher to RegexpMatches for clarity.
4
- - Added noframes tag to rdoc index to assist Google.
5
-
6
- = 0.5.4 (r166)
7
-
8
- - Added matches parameter matcher for matching regular expressions.
9
-
10
- = 0.5.3 (r165)
11
-
12
- - Attempt to fix packaging problems by switching to newer version (1.15.1) of gnutar and setting COPY_EXTENDED_ATTRIBUTES_DISABLE environment variable.
13
- - Removed unused ExpectationSequenceError exception.
14
- - Added instance_of and kind_of parameter matchers.
15
- - Added Google Webmaster meta tag to rdoc template header.
16
- - Put Google Webmaster meta tag in the right header i.e. the one for the index page.
17
-
18
- = 0.5.2 (r159)
19
-
20
- - Fix bug 11885 - "never doesn't work with stub_everything" submitted by Alexander Lang. In fixing this bug, also fixed undiscoverd bug where expected & actual invocation counts were being incorrectly reported which seems to have been introduced when fixes were added for invocation dispatch (see MockedMethodDispatchAcceptanceTest).
21
- - Previously when an expectation did not allow more invocations, it was treated as not matching. Now we prefer matching expectations which allow more invocations, but still match expectations which cannot allow more invocations. I think this may be overcomplicating things, but let's see how it goes.
22
-
23
- = 0.5.1 (r149)
24
-
25
- - Fixed bug #11583 "Mocha 0.5.0 throwing unexpected warnings". Also switched on ruby warning for all rake test tasks. Fixed majority of warnings, but some left to fix.
26
-
27
- = 0.5.0 (r147)
28
-
29
- - Parameter Matchers - I’ve added a few Hamcrest-style parameter matchers which are designed to be used inside Expectation#with. The following matchers are currently available: anything(), includes(), has_key(), has_value(), has_entry(), all_of() & any_of(). More to follow soon. The idea is eventually to get rid of the nasty parameter_block option on Expectation#with.
30
-
31
- object = mock()
32
- object.expects(:method).with(has_key('key_1'))
33
- object.method('key_1' => 1, 'key_2' => 2)
34
- # no verification error raised
35
-
36
- object = mock()
37
- object.expects(:method).with(has_key('key_1'))
38
- object.method('key_2' => 2)
39
- # verification error raised, because method was not called with Hash containing key: 'key_1'
40
-
41
- - Values Returned and Exceptions Raised on Consecutive Invocations - Allow multiple calls to Expectation#returns and Expectation#raises to build up a sequence of responses to invocations on the mock. Added syntactic sugar method Expectation#then to allow more readable expectations.
42
-
43
- object = mock()
44
- object.stubs(:method).returns(1, 2).then.raises(Exception).then.returns(4)
45
- object.method # => 1
46
- object.method # => 2
47
- object.method # => raises exception of class Exception
48
- object.method # => 4
49
-
50
- - Yields on Consecutive Invocations - Allow multiple calls to yields on single expectation to allow yield parameters to be specified for consecutive invocations.
51
-
52
- object = mock()
53
- object.stubs(:method).yields(1, 2).then.yields(3)
54
- object.method { |*values| p values } # => [1, 2]
55
- object.method { |*values| p values } # => [3]
56
-
57
- - Multiple Yields on Single Invocation - Added Expectation#multiple_yields to allow a mocked or stubbed method to yield multiple times for a single invocation.
58
-
59
- object = mock()
60
- object.stubs(:method).multiple_yields([1, 2], [3])
61
- object.method { |*values| p values } # => [1, 2] # => [3]
62
-
63
- - Invocation Dispatch - Expectations were already being matched in reverse order i.e. the most recently defined one was being found first. This is still the case, but we now stop matching an expectation when its maximum number of expected invocations is reached. c.f. JMock v1. A stub will never stop matching by default. Hopefully this means we can soon get rid of the need to pass a Proc to Expectation#returns.
64
-
65
- object = mock()
66
- object.stubs(:method).returns(2)
67
- object.expects(:method).once.returns(1)
68
- object.method # => 1
69
- object.method # => 2
70
- object.method # => 2
71
- # no verification error raised
72
-
73
- # The following should still work...
74
-
75
- Time.stubs(:now).returns(Time.parse('Mon Jan 01 00:00:00 UTC 2007'))
76
- Time.now # => Mon Jan 01 00:00:00 UTC 2007
77
- Time.stubs(:now).returns(Time.parse('Thu Feb 01 00:00:00 UTC 2007'))
78
- Time.now # => Thu Feb 01 00:00:00 UTC 2007
79
-
80
- - Deprecate passing an instance of Proc to Expectation#returns.
81
- - Explicitly include all Rakefile dependencies in project.
82
- - Fixed old Stubba example.
83
- - Fix so that it is possible for a stubbed method to raise an Interrupt exception without a message in Ruby 1.8.6
84
- - Added responds_like and quacks_like.
85
- - Capture standard object methods before Mocha adds any.
86
- - Added Expectation#once method to make interface less surprising.
87
- - Use Rake::TestTask to run tests. Created three separate tasks to run unit, integration & acceptance tests. Split inspect_test into one file per TestCase. Deleted superfluous all_tests file.
88
- - Fiddled with mocha_inspect and tests to give more sensible results on x86 platform.
89
- - Fixed bug #7834 "infinite_range.rb makes incorrect assumption about to_f" logged by James Moore.
90
-
91
- = 0.4.0 (r92)
92
-
93
- - Allow naming of mocks (patch from Chris Roos).
94
- - Specify multiple return values for consecutive calls.
95
- - Improved consistency of expectation error messages.
96
- - Allow mocking of Object instance methods e.g. kind_of?, type.
97
- - Provide aliased versions of #expects and #stubs to allow mocking of these methods.
98
- - Added at_least, at_most, at_most_once methods to expectation.
99
- - Allow expects and stubs to take a hash of method and return values.
100
- - Eliminate warning: "instance variable @yield not initialized" (patch from Xavier Shay).
101
- - Restore instance methods on partial mocks (patch from Chris Roos).
102
- - Allow stubbing of a method with non-word characters in its name (patch from Paul Battley).
103
- - Removed coupling to Test::Unit.
104
- - Allow specified exception instance to be raised (patch from Chris Roos).
105
- - Make mock object_id appear in hex like normal Ruby inspect (patch from Paul Battley).
106
- - Fix path to object.rb in rdoc rake task (patch from Tomas Pospisek).
107
- - Reverse order in which expectations are matched, so that last expectation is matched first. This allows e.g. a call to #stubs to be effectively overridden by a call to #expects (patch from Tobias Lutke).
108
- - Stubba & SmartTestCase modules incorporated into Mocha module so only need to require 'mocha' - no longer need to require 'stubba'.
109
- - AutoMocha removed.
110
-
111
- = 0.3.3
112
-
113
- - Quick bug fix to restore instance methods on partial mocks (for Kevin Clark).
114
-
115
- = 0.3.2
116
-
117
- - Examples added.
118
-
119
- = 0.3.1
120
-
121
- - Dual licensing with MIT license added.
122
-
123
- = 0.3.0
124
-
125
- * Rails plugin.
126
- * Auto-verify for expectations on concrete classes.
127
- * Include each expectation verification in the test result assertion count.
128
- * Filter out noise from assertion backtraces.
129
- * Point assertion backtrace to line where failing expectation was created.
130
- * New yields method for expectations.
131
- * Create stubs which stub all method calls.
132
- * Mocks now respond_to? expected methods.
133
-
134
- = 0.2.1
135
-
136
- * Rename MochaAcceptanceTest::Rover#move method to avoid conflict with Rake (in Ruby 1.8.4 only?)
137
-
138
- = 0.2.0
139
-
140
- * Small change to SetupAndTeardown#teardown_stubs suggested by Luke Redpath (http://www.lukeredpath.co.uk) to allow use of Stubba with RSpec (http://rspec.rubyforge.org).
141
- * Reorganized directory structure and extracted addition of setup and teardown methods into SmartTestCase mini-library.
142
- * Addition of auto-verify for Mocha (but not Stubba). This means there is more significance in the choice of expects or stubs in that any expects on a mock will automatically get verified.
143
-
144
- So instead of...
145
-
146
- wotsit = Mocha.new
147
- wotsit.expects(:thingummy).with(5).returns(10)
148
- doobrey = Doobrey.new(wotsit)
149
- doobrey.hoojamaflip
150
- wotsit.verify
151
-
152
- you need to do...
153
-
154
- wotsit = mock()
155
- wotsit.expects(:thingummy).with(5).returns(10)
156
- doobrey = Doobrey.new(wotsit)
157
- doobrey.hoojamaflip
158
- # no need to verify
159
-
160
- There are also shortcuts as follows...
161
-
162
- instead of...
163
-
164
- wotsit = Mocha.new
165
- wotsit.expects(:thingummy).returns(10)
166
- wotsit.expects(:summat).returns(25)
167
-
168
- you can have...
169
-
170
- wotsit = mock(:thingummy => 5, :summat => 25)
171
-
172
- and instead of...
173
-
174
- wotsit = Mocha.new
175
- wotsit.stubs(:thingummy).returns(10)
176
- wotsit.stubs(:summat).returns(25)
177
-
178
- you can have...
179
-
180
- wotsit = stub(:thingummy => 5, :summat => 25)
181
-
182
- = 0.1.2
183
-
184
- * Minor tweaks
185
-
186
- = 0.1.1
187
-
188
- * Initial release.
data/examples/misc.rb DELETED
@@ -1,44 +0,0 @@
1
- require 'test/unit'
2
- require 'rubygems'
3
- require 'mocha'
4
-
5
- class MiscExampleTest < Test::Unit::TestCase
6
-
7
- def test_mocking_a_class_method
8
- product = Product.new
9
- Product.expects(:find).with(1).returns(product)
10
- assert_equal product, Product.find(1)
11
- end
12
-
13
- def test_mocking_an_instance_method_on_a_real_object
14
- product = Product.new
15
- product.expects(:save).returns(true)
16
- assert product.save
17
- end
18
-
19
- def test_stubbing_instance_methods_on_real_objects
20
- prices = [stub(:pence => 1000), stub(:pence => 2000)]
21
- product = Product.new
22
- product.stubs(:prices).returns(prices)
23
- assert_equal [1000, 2000], product.prices.collect {|p| p.pence}
24
- end
25
-
26
- def test_stubbing_an_instance_method_on_all_instances_of_a_class
27
- Product.any_instance.stubs(:name).returns('stubbed_name')
28
- product = Product.new
29
- assert_equal 'stubbed_name', product.name
30
- end
31
-
32
- def test_traditional_mocking
33
- object = mock()
34
- object.expects(:expected_method).with(:p1, :p2).returns(:result)
35
- assert_equal :result, object.expected_method(:p1, :p2)
36
- end
37
-
38
- def test_shortcuts
39
- object = stub(:method1 => :result1, :method2 => :result2)
40
- assert_equal :result1, object.method1
41
- assert_equal :result2, object.method2
42
- end
43
-
44
- end
data/examples/mocha.rb DELETED
@@ -1,26 +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 'rubygems'
15
- require 'mocha'
16
-
17
- class EnterpriseTest < Test::Unit::TestCase
18
-
19
- def test_should_boldly_go
20
- dilithium = mock()
21
- dilithium.expects(:nuke).with(:anti_matter).at_least_once # auto-verified at end of test
22
- enterprise = Enterprise.new(dilithium)
23
- enterprise.go(2)
24
- end
25
-
26
- end
data/examples/stubba.rb DELETED
@@ -1,65 +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 'rubygems'
37
- require 'mocha'
38
-
39
- class OrderTest < Test::Unit::TestCase
40
-
41
- # illustrates stubbing instance method
42
- def test_should_calculate_shipping_cost_based_on_total_weight
43
- order = Order.new
44
- order.stubs(:total_weight).returns(10)
45
- assert_equal 60, order.shipping_cost
46
- end
47
-
48
- # illustrates stubbing class method
49
- def test_should_count_number_of_orders_shipped_after_specified_date
50
- now = Time.now; week_in_secs = 7 * 24 * 60 * 60
51
- order_1 = Order.new; order_1.shipped_on = now - 1 * week_in_secs
52
- order_2 = Order.new; order_2.shipped_on = now - 3 * week_in_secs
53
- Order.stubs(:find_all).returns([order_1, order_2])
54
- assert_equal 1, Order.number_shipped_since(now - 2 * week_in_secs)
55
- end
56
-
57
- # illustrates stubbing instance method for all instances of a class
58
- def test_should_calculate_value_of_unshipped_orders
59
- Order.stubs(:find_all).returns([Order.new, Order.new, Order.new])
60
- Order.any_instance.stubs(:shipped_on).returns(nil)
61
- Order.any_instance.stubs(:total_cost).returns(10)
62
- assert_equal 30, Order.unshipped_value
63
- end
64
-
65
- end
@@ -1,118 +0,0 @@
1
- require 'mocha/mock'
2
- require 'mocha/sequence'
3
-
4
- module Mocha # :nodoc:
5
-
6
- # Methods added to TestCase allowing creation of traditional mock objects.
7
- #
8
- # Mocks created this way will have their expectations automatically verified at the end of the test.
9
- #
10
- # See Mock for methods on mock objects.
11
- module AutoVerify
12
-
13
- def mocks # :nodoc:
14
- @mocks ||= []
15
- end
16
-
17
- def reset_mocks # :nodoc:
18
- @mocks = nil
19
- end
20
-
21
- # :call-seq: mock(name, &block) -> mock object
22
- # mock(expected_methods = {}, &block) -> mock object
23
- # mock(name, expected_methods = {}, &block) -> mock object
24
- #
25
- # Creates a mock object.
26
- #
27
- # +name+ is a +String+ identifier for the mock object.
28
- #
29
- # +expected_methods+ is a +Hash+ with expected method name symbols as keys and corresponding return values as values.
30
- #
31
- # +block+ is a block to be evaluated against the mock object instance, giving an alernative way to set up expectations & stubs.
32
- #
33
- # Note that (contrary to expectations set up by #stub) these expectations <b>must</b> be fulfilled during the test.
34
- # def test_product
35
- # product = mock('ipod_product', :manufacturer => 'ipod', :price => 100)
36
- # assert_equal 'ipod', product.manufacturer
37
- # assert_equal 100, product.price
38
- # # an error will be raised unless both Product#manufacturer and Product#price have been called
39
- # end
40
- def mock(*arguments, &block)
41
- name = arguments.shift if arguments.first.is_a?(String)
42
- expectations = arguments.shift || {}
43
- mock = Mock.new(name, &block)
44
- mock.expects(expectations)
45
- mocks << mock
46
- mock
47
- end
48
-
49
- # :call-seq: stub(name, &block) -> mock object
50
- # stub(stubbed_methods = {}, &block) -> mock object
51
- # stub(name, stubbed_methods = {}, &block) -> mock object
52
- #
53
- # Creates a mock object.
54
- #
55
- # +name+ is a +String+ identifier for the mock object.
56
- #
57
- # +stubbed_methods+ is a +Hash+ with stubbed method name symbols as keys and corresponding return values as values.
58
- #
59
- # +block+ is a block to be evaluated against the mock object instance, giving an alernative way to set up expectations & stubs.
60
- #
61
- # Note that (contrary to expectations set up by #mock) these expectations <b>need not</b> be fulfilled during the test.
62
- # def test_product
63
- # product = stub('ipod_product', :manufacturer => 'ipod', :price => 100)
64
- # assert_equal 'ipod', product.manufacturer
65
- # assert_equal 100, product.price
66
- # # an error will not be raised even if Product#manufacturer and Product#price have not been called
67
- # end
68
- def stub(*arguments, &block)
69
- name = arguments.shift if arguments.first.is_a?(String)
70
- expectations = arguments.shift || {}
71
- stub = Mock.new(name, &block)
72
- stub.stubs(expectations)
73
- mocks << stub
74
- stub
75
- end
76
-
77
- # :call-seq: stub_everything(name, &block) -> mock object
78
- # stub_everything(stubbed_methods = {}, &block) -> mock object
79
- # stub_everything(name, stubbed_methods = {}, &block) -> mock object
80
- #
81
- # Creates a mock object that accepts calls to any method.
82
- #
83
- # By default it will return +nil+ for any method call.
84
- #
85
- # +block+ is a block to be evaluated against the mock object instance, giving an alernative way to set up expectations & stubs.
86
- #
87
- # +name+ and +stubbed_methods+ work in the same way as for #stub.
88
- # def test_product
89
- # product = stub_everything('ipod_product', :price => 100)
90
- # assert_nil product.manufacturer
91
- # assert_nil product.any_old_method
92
- # assert_equal 100, product.price
93
- # end
94
- def stub_everything(*arguments, &block)
95
- name = arguments.shift if arguments.first.is_a?(String)
96
- expectations = arguments.shift || {}
97
- stub = Mock.new(name, &block)
98
- stub.stub_everything
99
- stub.stubs(expectations)
100
- mocks << stub
101
- stub
102
- end
103
-
104
- def verify_mocks # :nodoc:
105
- mocks.each { |mock| mock.verify { yield if block_given? } }
106
- end
107
-
108
- def teardown_mocks # :nodoc:
109
- reset_mocks
110
- end
111
-
112
- def sequence(name) # :nodoc:
113
- Sequence.new(name)
114
- end
115
-
116
- end
117
-
118
- end
@@ -1,66 +0,0 @@
1
- require 'mocha/metaclass'
2
-
3
- module Mocha
4
-
5
- class ClassMethod
6
-
7
- attr_reader :stubbee, :method
8
-
9
- def initialize(stubbee, method)
10
- @stubbee, @method = stubbee, method
11
- end
12
-
13
- def stub
14
- hide_original_method
15
- define_new_method
16
- end
17
-
18
- def unstub
19
- remove_new_method
20
- restore_original_method
21
- stubbee.reset_mocha
22
- end
23
-
24
- def mock
25
- stubbee.mocha
26
- end
27
-
28
- def hide_original_method
29
- stubbee.__metaclass__.class_eval "alias_method :#{hidden_method}, :#{method}" if stubbee.__metaclass__.method_defined?(method)
30
- end
31
-
32
- def define_new_method
33
- stubbee.__metaclass__.class_eval "def #{method}(*args, &block); mocha.method_missing(:#{method}, *args, &block); end"
34
- end
35
-
36
- def remove_new_method
37
- stubbee.__metaclass__.class_eval "remove_method :#{method}"
38
- end
39
-
40
- def restore_original_method
41
- stubbee.__metaclass__.class_eval "alias_method :#{method}, :#{hidden_method}; remove_method :#{hidden_method}" if stubbee.__metaclass__.method_defined?(hidden_method)
42
- end
43
-
44
- def hidden_method
45
- if RUBY_VERSION < '1.9'
46
- method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s[0]}_" }
47
- else
48
- method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s.ord}_" }
49
- end
50
- "__stubba__#{method_name}__stubba__"
51
- end
52
-
53
- def eql?(other)
54
- return false unless (other.class == self.class)
55
- (stubbee == other.stubbee) and (method == other.method)
56
- end
57
-
58
- alias_method :==, :eql?
59
-
60
- def to_s
61
- "#{stubbee}.#{method}"
62
- end
63
-
64
- end
65
-
66
- end
@@ -1,25 +0,0 @@
1
- class Range
2
-
3
- def self.at_least(minimum_value)
4
- Range.new(minimum_value, infinite)
5
- end
6
-
7
- def self.at_most(maximum_value)
8
- Range.new(-infinite, maximum_value, false)
9
- end
10
-
11
- def self.infinite
12
- 1/0.0
13
- end
14
-
15
- def mocha_inspect
16
- if first.respond_to?(:to_f) and first.to_f.infinite? then
17
- return "at most #{last}"
18
- elsif last.respond_to?(:to_f) and last.to_f.infinite? then
19
- return "at least #{first}"
20
- else
21
- to_s
22
- end
23
- end
24
-
25
- end
data/lib/mocha/is_a.rb DELETED
@@ -1,9 +0,0 @@
1
- class Object
2
-
3
- # :stopdoc:
4
-
5
- alias_method :__is_a__, :is_a?
6
-
7
- # :startdoc:
8
-
9
- end
@@ -1,7 +0,0 @@
1
- class Object
2
-
3
- def __metaclass__
4
- class << self; self; end
5
- end
6
-
7
- end
@@ -1,17 +0,0 @@
1
- require 'mocha/expectation'
2
-
3
- module Mocha # :nodoc:
4
-
5
- class MissingExpectation < Expectation # :nodoc:
6
-
7
- def verify
8
- message = error_message(0, 1)
9
- similar_expectations = @mock.expectations.similar(@method_matcher.expected_method_name)
10
- method_signatures = similar_expectations.map { |expectation| expectation.method_signature }
11
- message << "\nSimilar expectations:\n#{method_signatures.join("\n")}" unless method_signatures.empty?
12
- raise ExpectationError.new(message, backtrace)
13
- end
14
-
15
- end
16
-
17
- end
@@ -1,20 +0,0 @@
1
- module Mocha # :nodoc:
2
-
3
- class MultipleYields # :nodoc:
4
-
5
- attr_reader :parameter_groups
6
-
7
- def initialize(*parameter_groups)
8
- @parameter_groups = parameter_groups
9
- end
10
-
11
- def each
12
- @parameter_groups.each do |parameter_group|
13
- yield(parameter_group)
14
- end
15
- end
16
-
17
- end
18
-
19
- end
20
-
@@ -1,11 +0,0 @@
1
- module Mocha # :nodoc:
2
-
3
- class NoYields # :nodoc:
4
-
5
- def each
6
- end
7
-
8
- end
9
-
10
- end
11
-