diff --git a/lib/rspec/mocks/verifying_double.rb b/lib/rspec/mocks/verifying_double.rb index a511656e6..478e1ebbe 100644 --- a/lib/rspec/mocks/verifying_double.rb +++ b/lib/rspec/mocks/verifying_double.rb @@ -5,6 +5,14 @@ module Mocks # @api private module VerifyingDouble + def respond_to?(message, include_private=false) + if null_object? + __mock_proxy.method_reference[message].when_unimplemented { return false } + end + + super + end + def method_missing(message, *args, &block) # Null object conditional is an optimization. If not a null object, # validity of method expectations will have been checked at definition diff --git a/spec/rspec/mocks/verifying_double_spec.rb b/spec/rspec/mocks/verifying_double_spec.rb index 0ac37a2cb..217d5d77a 100644 --- a/spec/rspec/mocks/verifying_double_spec.rb +++ b/spec/rspec/mocks/verifying_double_spec.rb @@ -188,7 +188,10 @@ module Mocks o = instance_double('LoadedClass').as_null_object expect(o.defined_instance_method).to eq(o) + expect(o).to respond_to(:defined_instance_method) + prevents { o.undefined_method } + expect(o).not_to respond_to(:undefined_method) end end