Class: RR::DoubleInjection
- Inherits:
-
Object
- Object
- RR::DoubleInjection
- Defined in:
- lib/rr/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 ⇒ Object
readonly
Returns the value of attribute subject.
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.
- #call_original_method(*args, &block) ⇒ Object
-
#initialize(subject, method_name) ⇒ DoubleInjection
constructor
A new instance of DoubleInjection.
- #object_has_original_method? ⇒ Boolean
-
#register_double(double) ⇒ Object
RR::DoubleInjection#register_double adds the passed in Double into this DoubleInjection’s list of Double objects.
-
#reset ⇒ Object
RR::DoubleInjection#reset removes the injected dispatcher method.
-
#verify ⇒ Object
RR::DoubleInjection#verify verifies each Double TimesCalledExpectation are met.
Constructor Details
#initialize(subject, method_name) ⇒ DoubleInjection
Returns a new instance of DoubleInjection.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/rr/double_injection.rb', line 9 def initialize(subject, method_name) @subject = subject @method_name = method_name.to_sym if object_has_method?(method_name) begin .__send__(:alias_method, original_method_name, method_name) rescue NameError => e subject.send(method_name) .__send__(:alias_method, original_method_name, method_name) end end @doubles = [] end |
Instance Attribute Details
#doubles ⇒ Object (readonly)
Returns the value of attribute doubles.
7 8 9 |
# File 'lib/rr/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/double_injection.rb', line 7 def method_name @method_name end |
#subject ⇒ Object (readonly)
Returns the value of attribute subject.
7 8 9 |
# File 'lib/rr/double_injection.rb', line 7 def subject @subject 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.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rr/double_injection.rb', line 32 def bind define_implementation_placeholder returns_method = <<-METHOD def #{@method_name}(*args, &block) arguments = MethodArguments.new(args, block) __send__('#{placeholder_name}', arguments) end METHOD .class_eval(returns_method, __FILE__, __LINE__ - 5) end |
#call_original_method(*args, &block) ⇒ Object
64 65 66 |
# File 'lib/rr/double_injection.rb', line 64 def call_original_method(*args, &block) @subject.__send__(original_method_name, *args, &block) end |
#object_has_original_method? ⇒ Boolean
68 69 70 |
# File 'lib/rr/double_injection.rb', line 68 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.
25 26 27 |
# File 'lib/rr/double_injection.rb', line 25 def register_double(double) @doubles << double end |
#reset ⇒ Object
RR::DoubleInjection#reset removes the injected dispatcher method. It binds the original method implementation on the subject if one exists.
54 55 56 57 58 59 60 61 62 |
# File 'lib/rr/double_injection.rb', line 54 def reset .__send__(:remove_method, placeholder_name) if object_has_original_method? .__send__(:alias_method, @method_name, original_method_name) .__send__(:remove_method, original_method_name) else .__send__(:remove_method, @method_name) end end |
#verify ⇒ Object
RR::DoubleInjection#verify verifies each Double TimesCalledExpectation are met.
45 46 47 48 49 |
# File 'lib/rr/double_injection.rb', line 45 def verify @doubles.each do |double| double.verify end end |