Parent

RSpec::Mocks::AnyInstance::Recorder

Given a class `TheClass`, `TheClass.any_instance` returns a `Recorder`, which records stubs and message expectations for later playback on instances of `TheClass`.

Further constraints are stored in instances of [Chain](Chain).

@see AnyInstance @see Chain

Attributes

message_chains[R]

@private

Public Class Methods

new(klass) click to toggle source
    # File lib/rspec/mocks/any_instance/recorder.rb, line 16
16:         def initialize(klass)
17:           @message_chains = MessageChains.new
18:           @observed_methods = []
19:           @played_methods = {}
20:           @klass = klass
21:           @expectation_set = false
22:         end

Public Instance Methods

instance_that_received(method_name) click to toggle source

@private

     # File lib/rspec/mocks/any_instance/recorder.rb, line 103
103:         def instance_that_received(method_name)
104:           @played_methods[method_name]
105:         end
playback!(instance, method_name) click to toggle source

@private

     # File lib/rspec/mocks/any_instance/recorder.rb, line 95
 95:         def playback!(instance, method_name)
 96:           RSpec::Mocks::space.add(instance)
 97:           message_chains.playback!(instance, method_name)
 98:           @played_methods[method_name] = instance
 99:           received_expected_message!(method_name) if message_chains.has_expectation?(method_name)
100:         end
should_receive(method_name, &block) click to toggle source

Initializes the recording a message expectation to be played back against any instance of this object that invokes the submitted method.

@see Methods#should_receive

    # File lib/rspec/mocks/any_instance/recorder.rb, line 56
56:         def should_receive(method_name, &block)
57:           @expectation_set = true
58:           observe!(method_name)
59:           message_chains.add(method_name, ExpectationChain.new(method_name, &block))
60:         end
stop_all_observation!() click to toggle source

@private

    # File lib/rspec/mocks/any_instance/recorder.rb, line 90
90:         def stop_all_observation!
91:           @observed_methods.each {|method_name| restore_method!(method_name)}
92:         end
stub(method_name_or_method_map, &block) click to toggle source

Initializes the recording a stub to be played back against any instance of this object that invokes the submitted method.

@see Methods#stub

    # File lib/rspec/mocks/any_instance/recorder.rb, line 28
28:         def stub(method_name_or_method_map, &block)
29:           if method_name_or_method_map.is_a?(Hash)
30:             method_name_or_method_map.each do |method_name, return_value|
31:               stub(method_name).and_return(return_value)
32:             end
33:           else
34:             observe!(method_name_or_method_map)
35:             message_chains.add(method_name_or_method_map, StubChain.new(method_name_or_method_map, &block))
36:           end
37:         end
stub!(*) click to toggle source

@private

    # File lib/rspec/mocks/any_instance/recorder.rb, line 85
85:         def stub!(*)
86:           raise "stub! is not supported on any_instance. Use stub instead."
87:         end
stub_chain(*method_names_and_optional_return_values, &block) click to toggle source

Initializes the recording a stub chain to be played back against any instance of this object that invokes the method matching the first argument.

@see Methods#stub_chain

    # File lib/rspec/mocks/any_instance/recorder.rb, line 44
44:         def stub_chain(*method_names_and_optional_return_values, &block)
45:           normalize_chain(*method_names_and_optional_return_values) do |method_name, args|
46:             observe!(method_name)
47:             message_chains.add(method_name, StubChainChain.new(*args, &block))
48:           end
49:         end
unstub(method_name) click to toggle source

Removes any previously recorded stubs, stub_chains or message expectations that use `method_name`.

@see Methods#unstub

    # File lib/rspec/mocks/any_instance/recorder.rb, line 66
66:         def unstub(method_name)
67:           unless @observed_methods.include?(method_name.to_sym)
68:             raise RSpec::Mocks::MockExpectationError, "The method `#{method_name}` was not stubbed or was already unstubbed"
69:           end
70:           message_chains.remove_stub_chains_for!(method_name)
71:           stop_observing!(method_name) unless message_chains.has_expectation?(method_name)
72:         end
verify() click to toggle source

@api private

Used internally to verify that message expectations have been fulfilled.

    # File lib/rspec/mocks/any_instance/recorder.rb, line 78
78:         def verify
79:           if @expectation_set && !message_chains.all_expectations_fulfilled?
80:             raise RSpec::Mocks::MockExpectationError, "Exactly one instance should have received the following message(s) but didn't: #{message_chains.unfulfilled_expectations.sort.join(', ')}"
81:           end
82:         end

Private Instance Methods

already_observing?(method_name) click to toggle source
     # File lib/rspec/mocks/any_instance/recorder.rb, line 163
163:         def already_observing?(method_name)
164:           @observed_methods.include?(method_name)
165:         end
backup_method!(method_name) click to toggle source
     # File lib/rspec/mocks/any_instance/recorder.rb, line 147
147:         def backup_method!(method_name)
148:           alias_method_name = build_alias_method_name(method_name)
149:           @klass.class_eval do
150:             alias_method alias_method_name, method_name
151:           end if public_protected_or_private_method_defined?(method_name)
152:         end
build_alias_method_name(method_name) click to toggle source
     # File lib/rspec/mocks/any_instance/recorder.rb, line 128
128:         def build_alias_method_name(method_name)
129:           "__#{method_name}_without_any_instance__"
130:         end
mark_invoked!(method_name) click to toggle source
     # File lib/rspec/mocks/any_instance/recorder.rb, line 179
179:         def mark_invoked!(method_name)
180:           backup_method!(method_name)
181:           @klass.class_eval(            def #{method_name}(*args, &blk)              method_name = :#{method_name}              invoked_instance = self.class.__recorder.instance_that_received(method_name)              raise RSpec::Mocks::MockExpectationError, "The message '#{method_name}' was received by \#{self.inspect} but has already been received by \#{invoked_instance}"            end, __FILE__, __LINE__)
182:         end
normalize_chain(*args) click to toggle source
     # File lib/rspec/mocks/any_instance/recorder.rb, line 109
109:         def normalize_chain(*args)
110:           args.shift.to_s.split('.').map {|s| s.to_sym}.reverse.each {|a| args.unshift a}
111:           yield args.first, args
112:         end
observe!(method_name) click to toggle source
     # File lib/rspec/mocks/any_instance/recorder.rb, line 167
167:         def observe!(method_name)
168:           stop_observing!(method_name) if already_observing?(method_name)
169:           @observed_methods << method_name
170:           backup_method!(method_name)
171:           @klass.class_eval(            def #{method_name}(*args, &blk)              self.class.__recorder.playback!(self, :#{method_name})              self.__send__(:#{method_name}, *args, &blk)            end, __FILE__, __LINE__)
172:         end
public_protected_or_private_method_defined?(method_name) click to toggle source
     # File lib/rspec/mocks/any_instance/recorder.rb, line 154
154:         def public_protected_or_private_method_defined?(method_name)
155:           @klass.method_defined?(method_name) || @klass.private_method_defined?(method_name)
156:         end
received_expected_message!(method_name) click to toggle source
     # File lib/rspec/mocks/any_instance/recorder.rb, line 114
114:         def received_expected_message!(method_name)
115:           message_chains.received_expected_message!(method_name)
116:           restore_method!(method_name)
117:           mark_invoked!(method_name)
118:         end
remove_dummy_method!(method_name) click to toggle source
     # File lib/rspec/mocks/any_instance/recorder.rb, line 141
141:         def remove_dummy_method!(method_name)
142:           @klass.class_eval do
143:             remove_method method_name
144:           end
145:         end
restore_method!(method_name) click to toggle source
     # File lib/rspec/mocks/any_instance/recorder.rb, line 120
120:         def restore_method!(method_name)
121:           if public_protected_or_private_method_defined?(build_alias_method_name(method_name))
122:             restore_original_method!(method_name)
123:           else
124:             remove_dummy_method!(method_name)
125:           end
126:         end
restore_original_method!(method_name) click to toggle source
     # File lib/rspec/mocks/any_instance/recorder.rb, line 132
132:         def restore_original_method!(method_name)
133:           alias_method_name = build_alias_method_name(method_name)
134:           @klass.class_eval do
135:             remove_method method_name
136:             alias_method  method_name, alias_method_name
137:             remove_method alias_method_name
138:           end
139:         end
stop_observing!(method_name) click to toggle source
     # File lib/rspec/mocks/any_instance/recorder.rb, line 158
158:         def stop_observing!(method_name)
159:           restore_method!(method_name)
160:           @observed_methods.delete(method_name)
161:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.