diff --git a/lib/mocha/mock.rb b/lib/mocha/mock.rb index 46d65367e..cd3840d2c 100644 --- a/lib/mocha/mock.rb +++ b/lib/mocha/mock.rb @@ -108,8 +108,16 @@ class Mock # object = mock() # object.expects(:expected_method_one).returns(:result_one) # object.expects(:expected_method_two).returns(:result_two) - def expects(method_name_or_hash, backtrace = nil) - anticipates(method_name_or_hash, backtrace) + def expects(method_name_or_hash, backtrace = nil, object = Mock.new(@mockery)) + ExpectationSetting.new(Array(method_name_or_hash).map do |*args| + args = args.flatten + method_name = args.shift + Mockery.instance.stub_method(object, method_name) unless object.is_a?(Mock) + ensure_method_not_already_defined(method_name) + expectation = Expectation.new(self, method_name, backtrace) + expectation.returns(args.shift) unless args.empty? + @expectations.add(expectation) + end) end # Adds an expectation that the specified method may be called any number of times with any parameters. @@ -138,7 +146,7 @@ def expects(method_name_or_hash, backtrace = nil) # object.stubs(:stubbed_method_one).returns(:result_one) # object.stubs(:stubbed_method_two).returns(:result_two) def stubs(method_name_or_hash, backtrace = nil) - anticipates(method_name_or_hash, backtrace).at_least(0) + expects(method_name_or_hash, backtrace).at_least(0) end # Removes the specified stubbed methods (added by calls to {#expects} or {#stubs}) and all expectations associated with them. @@ -355,18 +363,5 @@ def ensure_method_not_already_defined(method_name) def any_expectations? @expectations.any? end - - # @private - def anticipates(method_name_or_hash, backtrace = nil, object = Mock.new(@mockery)) - ExpectationSetting.new(Array(method_name_or_hash).map do |*args| - args = args.flatten - method_name = args.shift - Mockery.instance.stub_method(object, method_name) unless object.is_a?(Mock) - ensure_method_not_already_defined(method_name) - expectation = Expectation.new(self, method_name, backtrace) - expectation.returns(args.shift) unless args.empty? - @expectations.add(expectation) - end) - end end end diff --git a/lib/mocha/object_methods.rb b/lib/mocha/object_methods.rb index a2c7ee851..6cd50d069 100644 --- a/lib/mocha/object_methods.rb +++ b/lib/mocha/object_methods.rb @@ -76,7 +76,10 @@ def expects(expected_methods_vs_return_values) if expected_methods_vs_return_values.to_s =~ /the[^a-z]*spanish[^a-z]*inquisition/i raise ExpectationErrorFactory.build('NOBODY EXPECTS THE SPANISH INQUISITION!') end - anticipates(expected_methods_vs_return_values) + if frozen? + raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}", caller) + end + mocha.expects(expected_methods_vs_return_values, caller, self) end # Adds an expectation that the specified method may be called any number of times with any parameters. @@ -108,7 +111,7 @@ def expects(expected_methods_vs_return_values) # # @see Mock#stubs def stubs(stubbed_methods_vs_return_values) - anticipates(stubbed_methods_vs_return_values).at_least(0) + expects(stubbed_methods_vs_return_values).at_least(0) end # Removes the specified stubbed methods (added by calls to {#expects} or {#stubs}) and all expectations associated with them. @@ -140,14 +143,5 @@ def unstub(*method_names) mockery.stubba.unstub(stubba_method_for(method_name)) end end - - private - - def anticipates(expected_methods_vs_return_values) - if frozen? - raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}", caller) - end - mocha.anticipates(expected_methods_vs_return_values, caller, self) - end end end