Object
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
@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
@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
Initializes the recording a message expectation to be played back against any instance of this object that invokes the submitted method.
# 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
@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
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
@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
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
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
@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
# File lib/rspec/mocks/any_instance/recorder.rb, line 163 163: def already_observing?(method_name) 164: @observed_methods.include?(method_name) 165: end
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.