Class: RR::Double

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(space, object, method_name) ⇒ Double

Returns a new instance of Double.



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

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

Instance Attribute Details

#method_nameObject (readonly)

Returns the value of attribute method_name.



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

def method_name
  @method_name
end

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

#scenariosObject (readonly)

Returns the value of attribute scenarios.



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

def scenarios
  @scenarios
end

#spaceObject (readonly)

Returns the value of attribute space.



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

def space
  @space
end

Instance Method Details

#bindObject

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



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

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

#original_methodObject

The original method of the object. It returns nil if the object does not have an original method.



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

def original_method
  return nil unless @object.respond_to?(original_method_name)
  return @object.method(original_method_name)
end

#register_scenario(scenario) ⇒ Object

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



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

def register_scenario(scenario)
  @scenarios << scenario
end

#resetObject

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



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

def reset
  meta.send(:remove_method, placeholder_name)
  if 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::Double#verify verifies each Scenario TimesCalledExpectation are met.



41
42
43
44
45
# File 'lib/rr/double.rb', line 41

def verify
  @scenarios.each do |scenario|
    scenario.verify
  end
end