Skip to content

Commit

Permalink
[Refactor]extract anticipates to DRY expects,stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
nitishr committed Dec 2, 2019
1 parent f9a62e3 commit 4a86bfb
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/mocha/object_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4a86bfb

Please sign in to comment.