Class: RR::Space
- Inherits:
-
Object
- Object
- RR::Space
- 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
-
#doubles ⇒ Object
readonly
Returns the value of attribute doubles.
-
#ordered_scenarios ⇒ Object
readonly
Returns the value of attribute ordered_scenarios.
-
#trim_backtrace ⇒ Object
Returns the value of attribute trim_backtrace.
Instance Method Summary collapse
-
#create_double(object, method_name) ⇒ Object
Reuses or creates, if none exists, a Double for the passed in object and method_name.
-
#create_scenario(double) ⇒ Object
Creates and registers a Scenario to be verified.
-
#do_not_allow_creator(subject, method_name = nil, &definition) ⇒ Object
Creates a DoNotAllowCreator.
-
#initialize ⇒ Space
constructor
A new instance of Space.
-
#mock_creator(subject, method_name = nil, &definition) ⇒ Object
Creates a MockCreator.
-
#mock_probe_creator(subject, method_name = nil, &definition) ⇒ Object
Creates a MockProbeCreator.
-
#register_ordered_scenario(scenario) ⇒ Object
Registers the ordered Scenario to be verified.
-
#reset ⇒ Object
Resets the registered Doubles and ordered Scenarios.
-
#reset_double(object, method_name) ⇒ Object
Resets the Double for the passed in object and method_name.
-
#stub_creator(subject, method_name = nil, &definition) ⇒ Object
Creates a StubCreator.
-
#stub_probe_creator(subject, method_name = nil, &definition) ⇒ Object
Creates a StubProbeCreator.
-
#verify_double(object, method_name) ⇒ Object
Verifies the Double for the passed in object and method_name.
-
#verify_doubles ⇒ Object
Verifies all the Double objects have met their TimesCalledExpectations.
-
#verify_ordered_scenario(scenario) ⇒ Object
Verifies that the passed in ordered Scenario is being called in the correct position.
Constructor Details
#initialize ⇒ Space
Returns a new instance of Space.
20 21 22 23 24 25 26 |
# File 'lib/rr/space.rb', line 20 def initialize @doubles = HashWithObjectIdKey.new do |hash, subject_object| hash.set_with_object_id(subject_object, HashWithObjectIdKey.new) end @ordered_scenarios = [] @trim_backtrace = false end |
Class Attribute Details
.instance ⇒ Object
7 8 9 |
# File 'lib/rr/space.rb', line 7 def instance @instance ||= new end |
Instance Attribute Details
#doubles ⇒ Object (readonly)
Returns the value of attribute doubles.
18 19 20 |
# File 'lib/rr/space.rb', line 18 def doubles @doubles end |
#ordered_scenarios ⇒ Object (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_backtrace ⇒ Object
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_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.
64 65 66 67 68 69 70 71 72 |
# File 'lib/rr/space.rb', line 64 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_scenario(double) ⇒ Object
Creates and registers a Scenario to be verified.
54 55 56 57 58 |
# File 'lib/rr/space.rb', line 54 def create_scenario(double) scenario = Scenario.new(self, double) double.register_scenario scenario scenario end |
#do_not_allow_creator(subject, method_name = nil, &definition) ⇒ Object
Creates a DoNotAllowCreator.
49 50 51 |
# File 'lib/rr/space.rb', line 49 def do_not_allow_creator(subject, method_name=nil, &definition) setup_creator DoNotAllowCreator, subject, method_name, definition end |
#mock_creator(subject, method_name = nil, &definition) ⇒ Object
Creates a MockCreator.
29 30 31 |
# File 'lib/rr/space.rb', line 29 def mock_creator(subject, method_name=nil, &definition) setup_creator MockCreator, subject, method_name, definition end |
#mock_probe_creator(subject, method_name = nil, &definition) ⇒ Object
Creates a MockProbeCreator.
39 40 41 |
# File 'lib/rr/space.rb', line 39 def mock_probe_creator(subject, method_name=nil, &definition) setup_creator MockProbeCreator, subject, method_name, definition end |
#register_ordered_scenario(scenario) ⇒ Object
Registers the ordered Scenario to be verified.
75 76 77 |
# File 'lib/rr/space.rb', line 75 def register_ordered_scenario(scenario) @ordered_scenarios << scenario end |
#reset ⇒ Object
Resets the registered Doubles and ordered Scenarios
107 108 109 110 |
# File 'lib/rr/space.rb', line 107 def reset reset_ordered_scenarios reset_doubles end |
#reset_double(object, method_name) ⇒ Object
Resets the Double for the passed in object and method_name.
120 121 122 123 124 |
# File 'lib/rr/space.rb', line 120 def reset_double(object, method_name) double = @doubles[object].delete(method_name) @doubles.delete(object) if @doubles[object].empty? double.reset end |
#stub_creator(subject, method_name = nil, &definition) ⇒ Object
Creates a StubCreator.
34 35 36 |
# File 'lib/rr/space.rb', line 34 def stub_creator(subject, method_name=nil, &definition) setup_creator StubCreator, subject, method_name, definition end |
#stub_probe_creator(subject, method_name = nil, &definition) ⇒ Object
Creates a StubProbeCreator.
44 45 46 |
# File 'lib/rr/space.rb', line 44 def stub_probe_creator(subject, method_name=nil, &definition) setup_creator StubProbeCreator, subject, method_name, definition end |
#verify_double(object, method_name) ⇒ Object
Verifies the Double for the passed in object and method_name.
113 114 115 116 117 |
# File 'lib/rr/space.rb', line 113 def verify_double(object, method_name) @doubles[object][method_name].verify ensure reset_double object, method_name end |
#verify_doubles ⇒ Object
Verifies all the Double objects have met their TimesCalledExpectations.
98 99 100 101 102 103 104 |
# File 'lib/rr/space.rb', line 98 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.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rr/space.rb', line 81 def verify_ordered_scenario(scenario) unless scenario.terminal? raise Errors::ScenarioOrderError, "Ordered Scenarios cannot have a NonTerminal TimesCalledExpectation" end unless @ordered_scenarios.first == scenario = Scenario.formatted_name(scenario.method_name, scenario.expected_arguments) << " called out of order in list\n" << Scenario.(@ordered_scenarios) raise Errors::ScenarioOrderError, end @ordered_scenarios.shift unless scenario.attempt? scenario end |