Skip to content

Commit

Permalink
replace anticipates with expects
Browse files Browse the repository at this point in the history
This adds an extra optional argument (object) to the signature of
expects. However, keeping it optional and undocumented may be a good
enough way of hiding it from the API.
  • Loading branch information
nitishr committed Feb 19, 2020
1 parent 583103b commit 74d8631
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
27 changes: 11 additions & 16 deletions lib/mocha/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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, 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
16 changes: 5 additions & 11 deletions lib/mocha/object_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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

0 comments on commit 74d8631

Please sign in to comment.