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.



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

def initialize
  @doubles = Hash.new {|hash, subject_object| hash[subject_object] = Hash.new}
  @ordered_scenarios = []
  @trim_backtrace = false
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

#trim_backtraceObject

Returns the value of attribute trim_backtrace.



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

def trim_backtrace
  @trim_backtrace
end

Instance Method Details

#create_do_not_allow_creator(subject, &definition) ⇒ Object

Creates a DoNotAllowCreator.



42
43
44
# File 'lib/rr/space.rb', line 42

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.



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

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.



27
28
29
# File 'lib/rr/space.rb', line 27

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

#create_probe_creator(subject, &definition) ⇒ Object

Creates a ProbeCreator.



37
38
39
# File 'lib/rr/space.rb', line 37

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

#create_scenario(double) ⇒ Object

Creates and registers a Scenario to be verified.



47
48
49
50
51
# File 'lib/rr/space.rb', line 47

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

#create_stub_creator(subject, &definition) ⇒ Object

Creates a StubCreator.



32
33
34
# File 'lib/rr/space.rb', line 32

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

#register_ordered_scenario(scenario) ⇒ Object

Registers the ordered Scenario to be verified.



68
69
70
# File 'lib/rr/space.rb', line 68

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.



107
108
109
110
111
# File 'lib/rr/space.rb', line 107

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.



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

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.



100
101
102
103
104
# File 'lib/rr/space.rb', line 100

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.



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

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.



74
75
76
77
78
# File 'lib/rr/space.rb', line 74

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