Parent

Class/Module Index [+]

Quicksearch

RR::Injections::DoubleInjection

RR::DoubleInjection is the binding of an subject and a method. A double_injection has 0 to many Double objects. Each Double has Argument Expectations and Times called Expectations.

Constants

MethodArguments

Attributes

doubles[R]
method_name[R]
subject_class[R]

Public Class Methods

create(subject, method_name) click to toggle source
# File lib/rr/injections/double_injection.rb, line 8
def create(subject, method_name)
  instances[subject][method_name.to_sym] ||= begin
    new(subject, method_name.to_sym, (class << subject; self; end)).bind
  end
end
exists?(subject, method_name) click to toggle source
# File lib/rr/injections/double_injection.rb, line 14
def exists?(subject, method_name)
  instances.include?(subject) && instances[subject].include?(method_name.to_sym)
end
instances() click to toggle source
# File lib/rr/injections/double_injection.rb, line 51
def instances
  @instances ||= HashWithObjectIdKey.new do |hash, subject_object|
    hash.set_with_object_id(subject_object, {})
  end
end
new(subject, method_name, subject_class) click to toggle source
# File lib/rr/injections/double_injection.rb, line 62
def initialize(subject, method_name, subject_class)
  @subject = subject
  @subject_class = subject_class
  @method_name = method_name.to_sym
  @doubles = []
  @bypass_bound_method = nil
end
reset() click to toggle source
# File lib/rr/injections/double_injection.rb, line 18
def reset
  instances.each do |subject, method_double_map|
    method_double_map.keys.each do |method_name|
      reset_double(subject, method_name)
    end
  end
end
reset_double(subject, method_name) click to toggle source

Resets the DoubleInjection for the passed in subject and method_name.

# File lib/rr/injections/double_injection.rb, line 45
def reset_double(subject, method_name)
  double_injection = Injections::DoubleInjection.instances[subject].delete(method_name)
  Injections::DoubleInjection.instances.delete(subject) if Injections::DoubleInjection.instances[subject].empty?
  double_injection.reset
end
verify(*subjects) click to toggle source
# File lib/rr/injections/double_injection.rb, line 26
def verify(*subjects)
  subjects = Injections::DoubleInjection.instances.keys if subjects.empty?
  subjects.each do |subject|
    instances.include?(subject) &&
      instances[subject].keys.each do |method_name|
        verify_double(subject, method_name)
      end &&
      instances.delete(subject)
  end
end
verify_double(subject, method_name) click to toggle source

Verifies the DoubleInjection for the passed in subject and method_name.

# File lib/rr/injections/double_injection.rb, line 38
def verify_double(subject, method_name)
  Injections::DoubleInjection.instances[subject][method_name].verify
ensure
  reset_double subject, method_name
end

Public Instance Methods

bind() click to toggle source

RR::DoubleInjection#bind injects a method that acts as a dispatcher that dispatches to the matching Double when the method is called.

# File lib/rr/injections/double_injection.rb, line 79
def bind
  if subject_respond_to_method?(method_name)
    if subject_has_method_defined?(method_name)
      if subject_is_proxy_for_method?(method_name)
        bind_method
      else
        bind_method_with_alias
      end
    else
      Injections::MethodMissingInjection.create(subject)
      Injections::SingletonMethodAddedInjection.create(subject)
    end
  else
    bind_method
  end
  self
end
bypass_bound_method() click to toggle source
# File lib/rr/injections/double_injection.rb, line 146
def bypass_bound_method
  @bypass_bound_method = true
  yield
ensure
  @bypass_bound_method = nil
end
dispatch_method(args, block) click to toggle source
# File lib/rr/injections/double_injection.rb, line 121
def dispatch_method(args, block)
  dispatch = MethodDispatches::MethodDispatch.new(self, args, block)
  if @bypass_bound_method
    dispatch.call_original_method
  else
    dispatch.call
  end
end
dispatch_method_missing(method_name, args, block) click to toggle source
# File lib/rr/injections/double_injection.rb, line 130
def dispatch_method_missing(method_name, args, block)
  MethodDispatches::MethodMissingDispatch.new(subject, method_name, args, block).call
end
original_method_alias_name() click to toggle source
# File lib/rr/injections/double_injection.rb, line 138
def original_method_alias_name
  "__rr__original_#{@method_name}"
end
original_method_missing_alias_name() click to toggle source
# File lib/rr/injections/double_injection.rb, line 142
def original_method_missing_alias_name
  MethodDispatches::MethodMissingDispatch.original_method_missing_alias_name
end
register_double(double) click to toggle source

RR::DoubleInjection#register_double adds the passed in Double into this DoubleInjection’s list of Double objects.

# File lib/rr/injections/double_injection.rb, line 72
def register_double(double)
  @doubles << double
end
reset() click to toggle source

It binds the original method implementation on the subject if one exists.

# File lib/rr/injections/double_injection.rb, line 109
def reset
  if subject_has_original_method?
    subject_class.__send__(:remove_method, method_name)
    subject_class.__send__(:alias_method, method_name, original_method_alias_name)
    subject_class.__send__(:remove_method, original_method_alias_name)
  else
    if subject_has_method_defined?(method_name)
      subject_class.__send__(:remove_method, method_name)
    end
  end
end
subject_has_original_method_missing?() click to toggle source
# File lib/rr/injections/double_injection.rb, line 134
def subject_has_original_method_missing?
  subject_respond_to_method?(original_method_missing_alias_name)
end
verify() click to toggle source

RR::DoubleInjection#verify verifies each Double TimesCalledExpectation are met.

# File lib/rr/injections/double_injection.rb, line 99
def verify
  @doubles.each do |double|
    double.verify
  end
end

Protected Instance Methods

bind_method() click to toggle source
# File lib/rr/injections/double_injection.rb, line 175
def bind_method
  subject = @subject.is_a?(Class) && !@subject.name.to_s.empty? ? @subject.name : "self"
  subject_class.class_eval(        def #{@method_name}(*args, &block)          arguments = MethodArguments.new(args, block)          RR::Injections::DoubleInjection.create(#{subject}, :#{@method_name}).dispatch_method(arguments.arguments, arguments.block)        end, __FILE__, __LINE__ + 1)
end
bind_method_with_alias() click to toggle source
# File lib/rr/injections/double_injection.rb, line 170
def bind_method_with_alias
  subject_class.__send__(:alias_method, original_method_alias_name, method_name)
  bind_method
end
deferred_bind_method() click to toggle source
# File lib/rr/injections/double_injection.rb, line 163
def deferred_bind_method
  unless subject_has_method_defined?(original_method_alias_name)
    bind_method_with_alias
  end
  @performed_deferred_bind = true
end
subject_is_proxy_for_method?(method_name_in_question) click to toggle source
# File lib/rr/injections/double_injection.rb, line 154
def subject_is_proxy_for_method?(method_name_in_question)
  !(
  class << @subject;
    self;
  end).
    instance_methods.
    detect {|method_name| method_name.to_sym == method_name_in_question.to_sym}
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.