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 108 def instance_that_received(method_name) @played_methods[method_name] end
@private
# File lib/rspec/mocks/any_instance/recorder.rb, line 100 def playback!(instance, method_name) RSpec::Mocks::space.add(instance) message_chains.playback!(instance, method_name) @played_methods[method_name] = instance received_expected_message!(method_name) if message_chains.has_expectation?(method_name) end
# File lib/rspec/mocks/any_instance/recorder.rb, line 62 def should_not_receive(method_name, &block) observe!(method_name) message_chains.add(method_name, NegativeExpectationChain.new(method_name, &block)) 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 def should_receive(method_name, &block) @expectation_set = true observe!(method_name) message_chains.add(method_name, PositiveExpectationChain.new(method_name, &block)) end
@private
# File lib/rspec/mocks/any_instance/recorder.rb, line 95 def stop_all_observation! @observed_methods.each {|method_name| restore_method!(method_name)} 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 def stub(method_name_or_method_map, &block) if method_name_or_method_map.is_a?(Hash) method_name_or_method_map.each do |method_name, return_value| stub(method_name).and_return(return_value) end else observe!(method_name_or_method_map) message_chains.add(method_name_or_method_map, StubChain.new(method_name_or_method_map, &block)) end end
@private
# File lib/rspec/mocks/any_instance/recorder.rb, line 90 def stub!(*) raise "stub! is not supported on any_instance. Use stub instead." 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 def stub_chain(*method_names_and_optional_return_values, &block) normalize_chain(*method_names_and_optional_return_values) do |method_name, args| observe!(method_name) message_chains.add(method_name, StubChainChain.new(*args, &block)) end 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 71 def unstub(method_name) unless @observed_methods.include?(method_name.to_sym) raise RSpec::Mocks::MockExpectationError, "The method `#{method_name}` was not stubbed or was already unstubbed" end message_chains.remove_stub_chains_for!(method_name) stop_observing!(method_name) unless message_chains.has_expectation?(method_name) end
@api private
Used internally to verify that message expectations have been fulfilled.
# File lib/rspec/mocks/any_instance/recorder.rb, line 83 def verify if @expectation_set && !message_chains.all_expectations_fulfilled? raise RSpec::Mocks::MockExpectationError, "Exactly one instance should have received the following message(s) but didn't: #{message_chains.unfulfilled_expectations.sort.join(', ')}" end end
Generated with the Darkfish Rdoc Generator 2.