lib/rspec/mocks/verifying_proxy.rb in rspec-mocks-3.2.1 vs lib/rspec/mocks/verifying_proxy.rb in rspec-mocks-3.3.0

- old
+ new

@@ -23,11 +23,12 @@ def ensure_implemented(method_name) return unless method_reference[method_name].unimplemented? @error_generator.raise_unimplemented_error( @doubled_module, - method_name + method_name, + @object ) end def ensure_publicly_implemented(method_name, _object) ensure_implemented(method_name) @@ -53,12 +54,12 @@ # # @private class VerifyingProxy < TestDoubleProxy include VerifyingProxyMethods - def initialize(object, order_group, name, doubled_module, method_reference_class) - super(object, order_group, name) + def initialize(object, order_group, doubled_module, method_reference_class) + super(object, order_group) @object = object @doubled_module = doubled_module @method_reference_class = method_reference_class # A custom method double is required to pass through a way to lookup @@ -69,11 +70,11 @@ end end def method_reference @method_reference ||= Hash.new do |h, k| - h[k] = @method_reference_class.new(@doubled_module, k) + h[k] = @method_reference_class.for(@doubled_module, k) end end def visibility_for(method_name) method_reference[method_name].visibility @@ -93,12 +94,16 @@ @doubled_module = DirectObjectReference.new(object) # A custom method double is required to pass through a way to lookup # methods to determine their parameters. @method_doubles = Hash.new do |h, k| - h[k] = VerifyingExistingMethodDouble.new(object, k, self) + h[k] = VerifyingExistingMethodDouble.for(object, k, self) end + + RSpec::Mocks.configuration.verifying_double_callbacks.each do |block| + block.call @doubled_module + end end def method_reference @method_doubles end @@ -156,18 +161,37 @@ @valid_method = object.respond_to?(method_name, true) # Trigger an eager find of the original method since if we find it any # later we end up getting a stubbed method with incorrect arity. - save_original_method! + save_original_implementation_callable! end def with_signature - yield Support::MethodSignature.new(original_method) + yield Support::MethodSignature.new(original_implementation_callable) end def unimplemented? !@valid_method + end + + def self.for(object, method_name, proxy) + if ClassNewMethodReference.applies_to?(method_name) { object } + VerifyingExistingClassNewMethodDouble + else + self + end.new(object, method_name, proxy) + end + end + + # Used in place of a `VerifyingExistingMethodDouble` for the specific case + # of mocking or stubbing a `new` method on a class. In this case, we substitute + # the method signature from `#initialize` since new's signature is just `*args`. + # + # @private + class VerifyingExistingClassNewMethodDouble < VerifyingExistingMethodDouble + def with_signature + yield Support::MethodSignature.new(object.instance_method(:initialize)) end end end end