Class: RR::Space

Inherits:
Object
  • Object
show all
Defined in:
lib/rr/space.rb

Overview

RR::Space is a Dependency Injection en.wikipedia.org/wiki/Dependency_injection and global state object for the RR framework. The RR::Space.instance is a singleton that holds the state.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpace

Returns a new instance of Space.



19
20
21
22
# File 'lib/rr/space.rb', line 19

def initialize
  @doubles = Hash.new {|hash, subject_object| hash[subject_object] = Hash.new}
  @ordered_scenarios = []
end

Class Attribute Details

.instanceObject



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

def instance
  @instance ||= new
end

Instance Attribute Details

#doublesObject (readonly)

Returns the value of attribute doubles.



18
19
20
# File 'lib/rr/space.rb', line 18

def doubles
  @doubles
end

#ordered_scenariosObject (readonly)

Returns the value of attribute ordered_scenarios.



18
19
20
# File 'lib/rr/space.rb', line 18

def ordered_scenarios
  @ordered_scenarios
end

Instance Method Details

#create_do_not_allow_creator(subject, &definition) ⇒ Object

Creates a DoNotAllowCreator.



40
41
42
# File 'lib/rr/space.rb', line 40

def create_do_not_allow_creator(subject, &definition)
  DoNotAllowCreator.new(self, subject, &definition)
end

#create_double(object, method_name) ⇒ Object

Reuses or creates, if none exists, a Double for the passed in object and method_name. When a Double is created, it binds the dispatcher to the object.



55
56
57
58
59
60
61
62
63
# File 'lib/rr/space.rb', line 55

def create_double(object, method_name)
  double = @doubles[object][method_name.to_sym]
  return double if double

  double = Double.new(self, object, method_name.to_sym)
  @doubles[object][method_name.to_sym] = double
  double.bind
  double
end

#create_mock_creator(subject, &definition) ⇒ Object

Creates a MockCreator.



25
26
27
# File 'lib/rr/space.rb', line 25

def create_mock_creator(subject, &definition)
  MockCreator.new(self, subject, &definition)
end

#create_probe_creator(subject, &definition) ⇒ Object

Creates a ProbeCreator.



35
36
37
# File 'lib/rr/space.rb', line 35

def create_probe_creator(subject, &definition)
  ProbeCreator.new(self, subject, &definition)
end

#create_scenario(double) ⇒ Object

Creates and registers a Scenario to be verified.



45
46
47
48
49
# File 'lib/rr/space.rb', line 45

def create_scenario(double)
  scenario = Scenario.new(self)
  double.register_scenario scenario
  scenario
end

#create_stub_creator(subject, &definition) ⇒ Object

Creates a StubCreator.



30
31
32
# File 'lib/rr/space.rb', line 30

def create_stub_creator(subject, &definition)
  StubCreator.new(self, subject, &definition)
end

#register_ordered_scenario(scenario) ⇒ Object

Registers the ordered Scenario to be verified.



66
67
68
# File 'lib/rr/space.rb', line 66

def register_ordered_scenario(scenario)
  @ordered_scenarios << scenario
end

#reset_double(object, method_name) ⇒ Object

Resets the Double for the passed in object and method_name.



105
106
107
108
109
# File 'lib/rr/space.rb', line 105

def reset_double(object, method_name)
  double = @doubles[object].delete(method_name)
  @doubles.delete(object) if @doubles[object].empty?
  double.reset
end

#reset_doublesObject

Resets the registered Doubles for the next test run.



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

def reset_doubles
  @doubles.each do |object, method_double_map|
    method_double_map.keys.each do |method_name|
      reset_double(object, method_name)
    end
  end
end

#verify_double(object, method_name) ⇒ Object

Verifies the Double for the passed in object and method_name.



98
99
100
101
102
# File 'lib/rr/space.rb', line 98

def verify_double(object, method_name)
  @doubles[object][method_name].verify
ensure
  reset_double object, method_name
end

#verify_doublesObject

Verifies all the Double objects have met their TimesCalledExpectations.



80
81
82
83
84
85
86
# File 'lib/rr/space.rb', line 80

def verify_doubles
  @doubles.each do |object, method_double_map|
    method_double_map.keys.each do |method_name|
      verify_double(object, method_name)
    end
  end
end

#verify_ordered_scenario(scenario) ⇒ Object

Verifies that the passed in ordered Scenario is being called in the correct position.



72
73
74
75
76
# File 'lib/rr/space.rb', line 72

def verify_ordered_scenario(scenario)
  raise ::RR::ScenarioOrderError unless @ordered_scenarios.first == scenario
  @ordered_scenarios.shift if scenario.times_called_verified?
  scenario
end