class Mocha::StubbedMethod

Constants

PrependedModule

Attributes

method_name[R]
stubbee[R]

Public Class Methods

new(stubbee, method_name) click to toggle source
# File lib/mocha/stubbed_method.rb, line 9
def initialize(stubbee, method_name)
  @stubbee = stubbee
  @original_method = nil
  @original_visibility = nil
  @method_name = method_name.to_sym
end

Public Instance Methods

define_new_method() click to toggle source
# File lib/mocha/stubbed_method.rb, line 56
def define_new_method
  self_in_scope = self
  method_name_in_scope = method_name
  stub_method_owner.send(:define_method, method_name) do |*args, &block|
    self_in_scope.mock.handle_method_call(method_name_in_scope, args, block)
  end
  retain_original_visibility(stub_method_owner)
end
hide_original_method() click to toggle source
# File lib/mocha/stubbed_method.rb, line 37
def hide_original_method
  return unless original_method_owner.__method_exists__?(method_name)
  store_original_method_visibility
  if use_prepended_module_for_stub_method?
    use_prepended_module_for_stub_method
  else
    begin
      store_original_method
    # rubocop:disable Lint/HandleExceptions
    rescue NameError
      # deal with nasties like ActiveRecord::Associations::AssociationProxy
    end
    # rubocop:enable Lint/HandleExceptions
    if stub_method_overwrites_original_method?
      remove_original_method_from_stubbee
    end
  end
end
matches?(other) click to toggle source
# File lib/mocha/stubbed_method.rb, line 81
def matches?(other)
  return false unless other.class == self.class
  (stubbee.object_id == other.stubbee.object_id) && (method_name == other.method_name)
end
mock() click to toggle source
# File lib/mocha/stubbed_method.rb, line 29
def mock
  mock_owner.mocha
end
remove_new_method() click to toggle source
# File lib/mocha/stubbed_method.rb, line 65
def remove_new_method
  stub_method_owner.send(:remove_method, method_name)
end
reset_mocha() click to toggle source
# File lib/mocha/stubbed_method.rb, line 33
def reset_mocha
  mock_owner.reset_mocha
end
restore_original_method() click to toggle source
# File lib/mocha/stubbed_method.rb, line 73
def restore_original_method
  return if use_prepended_module_for_stub_method?
  if stub_method_overwrites_original_method?
    original_method_owner.send(:define_method, method_name, @original_method)
  end
  retain_original_visibility(original_method_owner)
end
store_original_method() click to toggle source
# File lib/mocha/stubbed_method.rb, line 69
def store_original_method
  @original_method = stubbee_method(method_name)
end
stub() click to toggle source
# File lib/mocha/stubbed_method.rb, line 16
def stub
  hide_original_method
  define_new_method
end
to_s() click to toggle source
# File lib/mocha/stubbed_method.rb, line 88
def to_s
  "#{stubbee}.#{method_name}"
end
unstub() click to toggle source
# File lib/mocha/stubbed_method.rb, line 21
def unstub
  remove_new_method
  restore_original_method
  mock.unstub(method_name.to_sym)
  return if mock.any_expectations?
  reset_mocha
end

Private Instance Methods

remove_original_method_from_stubbee() click to toggle source
# File lib/mocha/stubbed_method.rb, line 107
def remove_original_method_from_stubbee
  original_method_owner.send(:remove_method, method_name)
end
retain_original_visibility(method_owner) click to toggle source
# File lib/mocha/stubbed_method.rb, line 94
def retain_original_visibility(method_owner)
  return unless @original_visibility
  Module.instance_method(@original_visibility).bind(method_owner).call(method_name)
end
store_original_method_visibility() click to toggle source
# File lib/mocha/stubbed_method.rb, line 99
def store_original_method_visibility
  @original_visibility = original_method_owner.__method_visibility__(method_name)
end
stub_method_overwrites_original_method?() click to toggle source
# File lib/mocha/stubbed_method.rb, line 103
def stub_method_overwrites_original_method?
  @original_method && @original_method.owner == original_method_owner
end
stub_method_owner() click to toggle source
# File lib/mocha/stubbed_method.rb, line 120
def stub_method_owner
  @stub_method_owner ||= original_method_owner
end
use_prepended_module_for_stub_method() click to toggle source
# File lib/mocha/stubbed_method.rb, line 115
def use_prepended_module_for_stub_method
  @stub_method_owner = PrependedModule.new
  original_method_owner.__send__ :prepend, @stub_method_owner
end
use_prepended_module_for_stub_method?() click to toggle source
# File lib/mocha/stubbed_method.rb, line 111
def use_prepended_module_for_stub_method?
  RUBY_V2_PLUS
end