class RSpec::Mocks::MethodDouble

@private

Attributes

method_name[R]

@private

Public Class Methods

new(object, method_name, proxy) click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 9
def initialize(object, method_name, proxy)
  @method_name = method_name
  @object = object
  @proxy = proxy

  @stashed_method = StashedInstanceMethod.new(object_singleton_class, @method_name)
  @method_is_proxied = false
  store(:expectations, [])
  store(:stubs, [])
end

Public Instance Methods

add_default_stub(*args, &implementation) click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 130
def add_default_stub(*args, &implementation)
  return if stubs.any?
  add_stub(*args, &implementation)
end
add_expectation(error_generator, expectation_ordering, expected_from, opts, &implementation) click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 102
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, @method_name, 1, opts, &implementation)
                end
  expectations << expectation
  expectation
end
add_negative_expectation(error_generator, expectation_ordering, expected_from, &implementation) click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 114
def add_negative_expectation(error_generator, expectation_ordering, expected_from, &implementation)
  configure_method
  expectation = NegativeMessageExpectation.new(error_generator, expectation_ordering, expected_from, @method_name, &implementation)
  expectations.unshift expectation
  expectation
end
add_stub(error_generator, expectation_ordering, expected_from, opts={}, &implementation) click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 122
def add_stub(error_generator, expectation_ordering, expected_from, opts={}, &implementation)
  configure_method
  stub = MessageExpectation.new(error_generator, expectation_ordering, expected_from, @method_name, :any, opts, &implementation)
  stubs.unshift stub
  stub
end
clear() click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 96
def clear
  expectations.clear
  stubs.clear
end
configure_method() click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 49
def configure_method
  RSpec::Mocks::space.add(@object) if RSpec::Mocks::space
  warn_if_nil_class
  @stashed_method.stash unless @method_is_proxied
  define_proxy_method
end
define_proxy_method() click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 57
      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
expectations() click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 21
def expectations
  self[:expectations]
end
object_singleton_class() click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 44
def object_singleton_class
  class << @object; self; end
end
proxy_for_nil_class?() click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 142
def proxy_for_nil_class?
  @object.nil?
end
raise_method_not_stubbed_error() click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 154
def raise_method_not_stubbed_error
  raise MockExpectationError, "The method `#{method_name}` was not stubbed or was already unstubbed" 
end
remove_stub() click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 136
def remove_stub
  raise_method_not_stubbed_error if stubs.empty?
  expectations.empty? ? reset : stubs.clear
end
reset() click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 89
def reset
  reset_nil_expectations_warning
  restore_original_method
  clear
end
reset_nil_expectations_warning() click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 159
def reset_nil_expectations_warning
  RSpec::Mocks::Proxy.warn_about_expectations_on_nil = true if proxy_for_nil_class?
end
restore_original_method() click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 75
def restore_original_method
  return unless @method_is_proxied

  object_singleton_class.__send__(:remove_method, @method_name)
  @stashed_method.restore
  @method_is_proxied = false
end
stubs() click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 26
def stubs
  self[:stubs]
end
verify() click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 84
def verify
  expectations.each {|e| e.verify_messages_received}
end
visibility() click to toggle source

@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
visibility_for_method() click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 70
def visibility_for_method
  "#{visibility} :#{method_name}"
end
warn_if_nil_class() click to toggle source

@private

# File lib/rspec/mocks/method_double.rb, line 147
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