Class: RR::Double
- Inherits:
-
Object
- Object
- RR::Double
- Defined in:
- lib/rr/double.rb
Overview
RR::Double is the binding of an object and a method. A double has 0 to many Scenario objects. Each Scenario has Argument Expectations and Times called Expectations.
Defined Under Namespace
Classes: MethodArguments
Instance Attribute Summary collapse
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#original_method ⇒ Object
readonly
Returns the value of attribute original_method.
-
#scenarios ⇒ Object
readonly
Returns the value of attribute scenarios.
-
#space ⇒ Object
readonly
Returns the value of attribute space.
Instance Method Summary collapse
-
#bind ⇒ Object
RR::Double#bind injects a method that acts as a dispatcher that dispatches to the matching Scenario when the method is called.
-
#initialize(space, object, method_name) ⇒ Double
constructor
A new instance of Double.
-
#register_scenario(scenario) ⇒ Object
RR::Double#register_scenario adds the passed in Scenario into this Double’s list of Scenario objects.
-
#reset ⇒ Object
RR::Double#reset removes the injected dispatcher method.
-
#verify ⇒ Object
RR::Double#verify verifies each Scenario TimesCalledExpectation are met.
Constructor Details
#initialize(space, object, method_name) ⇒ Double
Returns a new instance of Double.
9 10 11 12 13 14 15 |
# File 'lib/rr/double.rb', line 9 def initialize(space, object, method_name) @space = space @object = object @method_name = method_name.to_sym @original_method = object.method(method_name) if @object.methods.include?(method_name.to_s) @scenarios = [] end |
Instance Attribute Details
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
7 8 9 |
# File 'lib/rr/double.rb', line 7 def method_name @method_name end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
7 8 9 |
# File 'lib/rr/double.rb', line 7 def object @object end |
#original_method ⇒ Object (readonly)
Returns the value of attribute original_method.
7 8 9 |
# File 'lib/rr/double.rb', line 7 def original_method @original_method end |
#scenarios ⇒ Object (readonly)
Returns the value of attribute scenarios.
7 8 9 |
# File 'lib/rr/double.rb', line 7 def scenarios @scenarios end |
#space ⇒ Object (readonly)
Returns the value of attribute space.
7 8 9 |
# File 'lib/rr/double.rb', line 7 def space @space end |
Instance Method Details
#bind ⇒ Object
RR::Double#bind injects a method that acts as a dispatcher that dispatches to the matching Scenario when the method is called.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rr/double.rb', line 26 def bind define_implementation_placeholder returns_method = <<-METHOD def #{@method_name}(*args, &block) arguments = MethodArguments.new(args, block) #{placeholder_name}(arguments) end METHOD .class_eval(returns_method, __FILE__, __LINE__ - 5) end |
#register_scenario(scenario) ⇒ Object
RR::Double#register_scenario adds the passed in Scenario into this Double’s list of Scenario objects.
19 20 21 |
# File 'lib/rr/double.rb', line 19 def register_scenario(scenario) @scenarios << scenario end |
#reset ⇒ Object
RR::Double#reset removes the injected dispatcher method. It binds the original method implementation on the object if one exists.
48 49 50 51 52 53 54 55 |
# File 'lib/rr/double.rb', line 48 def reset .send(:remove_method, placeholder_name) if @original_method .send(:define_method, @method_name, &@original_method) else .send(:remove_method, @method_name) end end |
#verify ⇒ Object
RR::Double#verify verifies each Scenario TimesCalledExpectation are met.
39 40 41 42 43 |
# File 'lib/rr/double.rb', line 39 def verify @scenarios.each do |scenario| scenario.verify end end |