@private
@private
@private
@private
@private
# File lib/rspec/mocks/method_double.rb, line 9 def initialize(object, method_name, proxy) @method_name = method_name @object = object @proxy = proxy @method_stasher = InstanceMethodStasher.new(object_singleton_class, @method_name) @method_is_proxied = false store(:expectations, []) store(:stubs, []) end
@private
# File lib/rspec/mocks/method_double.rb, line 207 def add_default_stub(*args, &implementation) return if stubs.any? add_stub(*args, &implementation) end
@private
# File lib/rspec/mocks/method_double.rb, line 176 def add_expectation(error_generator, expectation_ordering, expected_from, opts, &implementation) configure_method expectation = if existing_stub = stubs.first existing_stub.build_child(expected_from, 1, opts, &implementation) else MessageExpectation.new(error_generator, expectation_ordering, expected_from, self, 1, opts, &implementation) end expectations << expectation expectation end
@private
# File lib/rspec/mocks/method_double.rb, line 189 def add_negative_expectation(error_generator, expectation_ordering, expected_from, &implementation) configure_method expectation = NegativeMessageExpectation.new(error_generator, expectation_ordering, expected_from, self, &implementation) expectations.unshift expectation expectation end
@private
# File lib/rspec/mocks/method_double.rb, line 198 def add_stub(error_generator, expectation_ordering, expected_from, opts={}, &implementation) configure_method stub = MessageExpectation.new(error_generator, expectation_ordering, expected_from, self, :any, opts, &implementation) stubs.unshift stub stub end
@private
# File lib/rspec/mocks/method_double.rb, line 170 def clear expectations.clear stubs.clear end
@private
# File lib/rspec/mocks/method_double.rb, line 123 def configure_method RSpec::Mocks::space.add(@object) if RSpec::Mocks::space warn_if_nil_class @method_stasher.stash unless @method_is_proxied define_proxy_method end
@private
# File lib/rspec/mocks/method_double.rb, line 131 def define_proxy_method return if @method_is_proxied object_singleton_class.class_eval " def #{@method_name}(*args, &block) __mock_proxy.message_received :#{@method_name}, *args, &block end #{visibility_for_method} ", __FILE__, __LINE__ + 1 @method_is_proxied = true end
@private
# File lib/rspec/mocks/method_double.rb, line 21 def expectations self[:expectations] end
@private
# File lib/rspec/mocks/method_double.rb, line 113 def method_handle_for(object, method_name) OBJECT_METHOD_METHOD.bind(object).call(method_name) end
@private
# File lib/rspec/mocks/method_double.rb, line 118 def object_singleton_class class << @object; self; end end
@private
# File lib/rspec/mocks/method_double.rb, line 44 def original_method if @method_stasher.method_is_stashed? # Example: a singleton method defined on @object method_handle_for(@object, @method_stasher.stashed_method_name) else begin # Example: an instance method defined on @object's class. @object.class.instance_method(@method_name).bind(@object) rescue NameError raise unless @object.respond_to?(:superclass) # Example: a singleton method defined on @object's superclass. # # Note: we have to give precedence to instance methods # defined on @object's class, because in a case like: # # `klass.should_receive(:new).and_call_original` # # ...we want `Class#new` bound to `klass` (which will return # an instance of `klass`), not `klass.superclass.new` (which # would return an instance of `klass.superclass`). original_method_from_superclass end end rescue NameError # We have no way of knowing if the object's method_missing # will handle this message or not...but we can at least try. # If it's not handled, a `NoMethodError` will be raised, just # like normally. Proc.new do |*args, &block| @object.__send__(:method_missing, @method_name, *args, &block) end end
@private
# File lib/rspec/mocks/method_double.rb, line 80 def original_method_from_superclass @object.superclass. singleton_class. instance_method(@method_name). bind(@object) end
@private
# File lib/rspec/mocks/method_double.rb, line 219 def proxy_for_nil_class? @object.nil? end
@private
# File lib/rspec/mocks/method_double.rb, line 231 def raise_method_not_stubbed_error raise MockExpectationError, "The method `#{method_name}` was not stubbed or was already unstubbed" end
@private
# File lib/rspec/mocks/method_double.rb, line 213 def remove_stub raise_method_not_stubbed_error if stubs.empty? expectations.empty? ? reset : stubs.clear end
@private
# File lib/rspec/mocks/method_double.rb, line 163 def reset reset_nil_expectations_warning restore_original_method clear end
@private
# File lib/rspec/mocks/method_double.rb, line 236 def reset_nil_expectations_warning RSpec::Mocks::Proxy.warn_about_expectations_on_nil = true if proxy_for_nil_class? end
@private
# File lib/rspec/mocks/method_double.rb, line 149 def restore_original_method return unless @method_is_proxied object_singleton_class.__send__(:remove_method, @method_name) @method_stasher.restore @method_is_proxied = false end
@private
# File lib/rspec/mocks/method_double.rb, line 26 def stubs self[:stubs] end
@private
# File lib/rspec/mocks/method_double.rb, line 158 def verify expectations.each {|e| e.verify_messages_received} end
@private
# File lib/rspec/mocks/method_double.rb, line 31 def visibility if TestDouble === @object 'public' elsif object_singleton_class.private_method_defined?(@method_name) 'private' elsif object_singleton_class.protected_method_defined?(@method_name) 'protected' else 'public' end end
@private
# File lib/rspec/mocks/method_double.rb, line 144 def visibility_for_method "#{visibility} :#{method_name}" end
@private
# File lib/rspec/mocks/method_double.rb, line 224 def warn_if_nil_class if proxy_for_nil_class? & RSpec::Mocks::Proxy.warn_about_expectations_on_nil Kernel.warn("An expectation of :#{@method_name} was set on nil. Called from #{caller[4]}. Use allow_message_expectations_on_nil to disable warnings.") end end