From 9930e5abf63a83abf7daefcdac84602029270343 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 21 Feb 2020 12:38:14 +0000 Subject: [PATCH] Refactor: inline anticipates into expects This reduces the surface area of the internal API exposed to users as suggested in #467 --- lib/mocha/mock.rb | 25 ++++++++++--------------- lib/mocha/object_methods.rb | 20 +++++++------------- 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/lib/mocha/mock.rb b/lib/mocha/mock.rb index 37a66bd0a..850c32ca2 100644 --- a/lib/mocha/mock.rb +++ b/lib/mocha/mock.rb @@ -109,7 +109,15 @@ class 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) + ExpectationSetting.new(Array(method_name_or_hash).map do |*args| + args = args.flatten + method_name = args.shift + yield method_name if block_given? + 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) - ExpectationSetting.new(Array(method_name_or_hash).map do |*args| - args = args.flatten - method_name = args.shift - yield method_name if block_given? - 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 cb3e6f8a6..1464552c7 100644 --- a/lib/mocha/object_methods.rb +++ b/lib/mocha/object_methods.rb @@ -73,7 +73,12 @@ def stubba_method_for(method_name) # # @see Mock#expects def expects(expected_methods_vs_return_values) - 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) do |method_name| + Mockery.instance.stub_method(self, method_name) + end end # Adds an expectation that the specified method may be called any number of times with any parameters. @@ -105,7 +110,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. @@ -137,16 +142,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) do |method_name| - Mockery.instance.stub_method(self, method_name) - end - end end end