From 4a86bfb3133a1987c1278524642ca192d47ebb34 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Mon, 2 Dec 2019 10:05:50 +0000 Subject: [PATCH] [Refactor]extract anticipates to DRY expects,stubs --- lib/mocha/object_methods.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/mocha/object_methods.rb b/lib/mocha/object_methods.rb index 21de31070..a3c01d05f 100644 --- a/lib/mocha/object_methods.rb +++ b/lib/mocha/object_methods.rb @@ -76,8 +76,7 @@ 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 - error_if_frozen - mocha.anticipates(expected_methods_vs_return_values, caller, self) + anticipates(expected_methods_vs_return_values) end # Adds an expectation that the specified method may be called any number of times with any parameters. @@ -109,8 +108,7 @@ def expects(expected_methods_vs_return_values) # # @see Mock#stubs def stubs(stubbed_methods_vs_return_values) - error_if_frozen - mocha.anticipates(stubbed_methods_vs_return_values, caller, self) { |expectation| expectation.at_least(0) } + anticipates(stubbed_methods_vs_return_values) { |expectation| expectation.at_least(0) } end # Removes the specified stubbed methods (added by calls to {#expects} or {#stubs}) and all expectations associated with them. @@ -145,6 +143,11 @@ def unstub(*method_names) private + def anticipates(expected_methods_vs_return_values, &block) + error_if_frozen + mocha.anticipates(expected_methods_vs_return_values, caller, self, &block) + end + def error_if_frozen if frozen? raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}", caller)