Class: RR::DoubleInjection

Inherits:
Object
  • Object
show all
Defined in:
lib/rr/double_injection.rb

Overview

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

Defined Under Namespace

Classes: MethodArguments

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, method_name) ⇒ DoubleInjection

Returns a new instance of DoubleInjection.



9
10
11
12
13
14
15
16
# File 'lib/rr/double_injection.rb', line 9

def initialize(object, method_name)
  @object = object
  @method_name = method_name.to_sym
  if object_has_method?(method_name)
    meta.send(:alias_method, original_method_name, method_name)
  end
  @doubles = []
end

Instance Attribute Details

#doublesObject (readonly)

Returns the value of attribute doubles.



7
8
9
# File 'lib/rr/double_injection.rb', line 7

def doubles
  @doubles
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



7
8
9
# File 'lib/rr/double_injection.rb', line 7

def method_name
  @method_name
end

#objectObject (readonly)

Returns the value of attribute object.



7
8
9
# File 'lib/rr/double_injection.rb', line 7

def object
  @object
end

Instance Method Details

#bindObject

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



27
28
29
30
31
32
33
34
35
36
# File 'lib/rr/double_injection.rb', line 27

def bind
  define_implementation_placeholder
  returns_method = <<-METHOD
    def #{@method_name}(*args, &block)
      arguments = MethodArguments.new(args, block)
      __send__('#{placeholder_name}', arguments)
    end
  METHOD
  meta.class_eval(returns_method, __FILE__, __LINE__ - 5)
end

#call_original_method(*args, &block) ⇒ Object



59
60
61
# File 'lib/rr/double_injection.rb', line 59

def call_original_method(*args, &block)
  @object.__send__(original_method_name, *args, &block)
end

#object_has_original_method?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/rr/double_injection.rb', line 63

def object_has_original_method?
  object_has_method?(original_method_name)
end

#register_double(double) ⇒ Object

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



20
21
22
# File 'lib/rr/double_injection.rb', line 20

def register_double(double)
  @doubles << double
end

#resetObject

RR::DoubleInjection#reset removes the injected dispatcher method. It binds the original method implementation on the object if one exists.



49
50
51
52
53
54
55
56
57
# File 'lib/rr/double_injection.rb', line 49

def reset
  meta.send(:remove_method, placeholder_name)
  if object_has_original_method?
    meta.send(:alias_method, @method_name, original_method_name)
    meta.send(:remove_method, original_method_name)
  else
    meta.send(:remove_method, @method_name)
  end
end

#verifyObject

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



40
41
42
43
44
# File 'lib/rr/double_injection.rb', line 40

def verify
  @doubles.each do |double|
    double.verify
  end
end