Class: RR::Injections::DoubleInjection
- 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
-
#doubles ⇒ Object
readonly
Returns the value of attribute doubles.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#subject_class ⇒ Object
readonly
Returns the value of attribute subject_class.
Attributes inherited from Injection
Instance Method Summary collapse
-
#bind ⇒ Object
RR::DoubleInjection#bind injects a method that acts as a dispatcher that dispatches to the matching Double when the method is called.
- #bypass_bound_method ⇒ Object
- #dispatch_method(args, block) ⇒ Object
- #dispatch_method_missing(method_name, args, block) ⇒ Object
-
#initialize(subject, method_name, subject_class) ⇒ DoubleInjection
constructor
A new instance of DoubleInjection.
- #original_method_alias_name ⇒ Object
- #original_method_missing_alias_name ⇒ Object
-
#register_double(double) ⇒ Object
RR::DoubleInjection#register_double adds the passed in Double into this DoubleInjection’s list of Double objects.
-
#reset ⇒ Object
It binds the original method implementation on the subject if one exists.
- #subject_has_original_method_missing? ⇒ Boolean
-
#verify ⇒ Object
RR::DoubleInjection#verify verifies each Double TimesCalledExpectation are met.
Methods inherited from Injection
#subject_has_method_defined?, #subject_has_original_method?
Methods included from Space::Reader
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
#doubles ⇒ Object (readonly)
Returns the value of attribute doubles.
7 8 9 |
# File 'lib/rr/injections/double_injection.rb', line 7 def doubles @doubles end |
#method_name ⇒ Object (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_class ⇒ Object (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
#bind ⇒ Object
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 41 42 43 44 |
# 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) if subject_is_proxy_for_method?(method_name) bind_method else bind_method_with_alias end else space.method_missing_injection(subject) space.singleton_method_added_injection(subject) end else bind_method end self end |
#bypass_bound_method ⇒ Object
94 95 96 97 98 99 |
# File 'lib/rr/injections/double_injection.rb', line 94 def bypass_bound_method @bypass_bound_method = true yield ensure @bypass_bound_method = nil end |
#dispatch_method(args, block) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/rr/injections/double_injection.rb', line 69 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
78 79 80 |
# File 'lib/rr/injections/double_injection.rb', line 78 def dispatch_method_missing(method_name, args, block) MethodDispatches::MethodMissingDispatch.new(subject, method_name, args, block).call end |
#original_method_alias_name ⇒ Object
86 87 88 |
# File 'lib/rr/injections/double_injection.rb', line 86 def original_method_alias_name "__rr__original_#{@method_name}" end |
#original_method_missing_alias_name ⇒ Object
90 91 92 |
# File 'lib/rr/injections/double_injection.rb', line 90 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 |
#reset ⇒ Object
It binds the original method implementation on the subject if one exists.
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rr/injections/double_injection.rb', line 57 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
82 83 84 |
# File 'lib/rr/injections/double_injection.rb', line 82 def subject_has_original_method_missing? subject_respond_to_method?(original_method_missing_alias_name) end |
#verify ⇒ Object
RR::DoubleInjection#verify verifies each Double TimesCalledExpectation are met.
48 49 50 51 52 |
# File 'lib/rr/injections/double_injection.rb', line 48 def verify @doubles.each do |double| double.verify end end |