diff --git a/lib/mocha/mock.rb b/lib/mocha/mock.rb index 46d65367e..4922684cd 100644 --- a/lib/mocha/mock.rb +++ b/lib/mocha/mock.rb @@ -357,11 +357,11 @@ def any_expectations? end # @private - def anticipates(method_name_or_hash, backtrace = nil, object = Mock.new(@mockery)) + def anticipates(method_name_or_hash, backtrace = nil, &block) 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) + yield method_name if block ensure_method_not_already_defined(method_name) expectation = Expectation.new(self, method_name, backtrace) expectation.returns(args.shift) unless args.empty? diff --git a/lib/mocha/object_methods.rb b/lib/mocha/object_methods.rb index a2c7ee851..4d5020339 100644 --- a/lib/mocha/object_methods.rb +++ b/lib/mocha/object_methods.rb @@ -147,7 +147,9 @@ 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) + mocha.anticipates(expected_methods_vs_return_values, caller) do |method_name| + Mockery.instance.stub_method(self, method_name) + end end end end