Class: RR::Injections::DoubleInjection

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

Overview

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.

Defined Under Namespace

Classes: MethodArguments

Instance Attribute Summary collapse

Attributes inherited from Injection

#subject

Instance Method Summary collapse

Methods inherited from Injection

#subject_has_method_defined?, #subject_has_original_method?

Methods included from Space::Reader

#space

Constructor Details

#initialize(subject, method_name, subject_class) ⇒ DoubleInjection

Returns a new instance of DoubleInjection.



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

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

Instance Attribute Details

#doublesObject (readonly)

Returns the value of attribute doubles.



7
8
9
# File 'lib/rr/injections/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/injections/double_injection.rb', line 7

def method_name
  @method_name
end

#subject_classObject (readonly)

Returns the value of attribute subject_class.



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

def subject_class
  @subject_class
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.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rr/injections/double_injection.rb', line 28

def bind
  if subject_respond_to_method?(method_name)
    if subject_has_method_defined?(method_name)
      bind_method_with_alias
    else
      space.method_missing_injection(subject)
      space.singleton_method_added_injection(subject)
    end
  else
    bind_method
  end
  self
end

#bypass_bound_methodObject



90
91
92
93
94
95
# File 'lib/rr/injections/double_injection.rb', line 90

def bypass_bound_method
  @bypass_bound_method = true
  yield
ensure
  @bypass_bound_method = nil
end

#dispatch_method(args, block) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/rr/injections/double_injection.rb', line 65

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) ⇒ Object



74
75
76
# File 'lib/rr/injections/double_injection.rb', line 74

def dispatch_method_missing(method_name, args, block)
  MethodDispatches::MethodMissingDispatch.new(subject, method_name, args, block).call
end

#original_method_alias_nameObject



82
83
84
# File 'lib/rr/injections/double_injection.rb', line 82

def original_method_alias_name
  "__rr__original_#{@method_name}"
end

#original_method_missing_alias_nameObject



86
87
88
# File 'lib/rr/injections/double_injection.rb', line 86

def original_method_missing_alias_name
  MethodDispatches::MethodMissingDispatch.original_method_missing_alias_name
end

#register_double(double) ⇒ Object

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



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

def register_double(double)
  @doubles << double
end

#resetObject

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



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rr/injections/double_injection.rb', line 53

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?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/rr/injections/double_injection.rb', line 78

def subject_has_original_method_missing?
  subject_respond_to_method?(original_method_missing_alias_name)
end

#verifyObject

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



44
45
46
47
48
# File 'lib/rr/injections/double_injection.rb', line 44

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