Skip to content

Commit

Permalink
Refactor: inline anticipates into expects
Browse files Browse the repository at this point in the history
This reduces the surface area of the internal API exposed to users as
suggested in #467
  • Loading branch information
nitishr committed Feb 21, 2020
1 parent 8a7af66 commit 9930e5a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 28 deletions.
25 changes: 10 additions & 15 deletions lib/mocha/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
20 changes: 7 additions & 13 deletions lib/mocha/object_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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

0 comments on commit 9930e5a

Please sign in to comment.