Class: RSpec::Mocks::AnyInstance::Recorder

### Home Classes Methods

### In Files

- lib/rspec/mocks/any_instance/recorder.rb

### Parent

Object

### Methods

- ::new
- #already_observing?
- #build_alias_method_name
- #expect_chain
- #instance_that_received
- #notify_received_message
- #playback!
- #should_not_receive
- #should_receive
- #stop_all_observation!
- #stop_observing!
- #stub
- #stub_chain
- #unstub
- #verify

### Class/Module Index

Quicksearch

- RSpec
- RSpec::Mocks
- RSpec::Mocks::AllowanceTarget
- RSpec::Mocks::AndReturnImplementation
- RSpec::Mocks::AndWrapOriginalImplementation
- RSpec::Mocks::AndYieldImplementation
- RSpec::Mocks::AnyInstance
- RSpec::Mocks::AnyInstance::Chain
- RSpec::Mocks::AnyInstance::Chain::Customizations
- RSpec::Mocks::AnyInstance::ErrorGenerator
- RSpec::Mocks::AnyInstance::ExpectChainChain
- RSpec::Mocks::AnyInstance::ExpectationChain
- RSpec::Mocks::AnyInstance::FluentInterfaceProxy
- RSpec::Mocks::AnyInstance::MessageChains
- RSpec::Mocks::AnyInstance::PositiveExpectationChain
- RSpec::Mocks::AnyInstance::Proxy
- RSpec::Mocks::AnyInstance::Recorder
- RSpec::Mocks::AnyInstance::StubChain
- RSpec::Mocks::AnyInstance::StubChainChain
- RSpec::Mocks::AnyInstanceAllowanceTarget
- RSpec::Mocks::AnyInstanceExpectationTarget
- RSpec::Mocks::ArgumentListMatcher
- RSpec::Mocks::ArgumentMatchers
- RSpec::Mocks::ArgumentMatchers::AnyArgMatcher
- RSpec::Mocks::ArgumentMatchers::AnyArgsMatcher
- RSpec::Mocks::ArgumentMatchers::ArrayIncludingMatcher
- RSpec::Mocks::ArgumentMatchers::BaseHashMatcher
- RSpec::Mocks::ArgumentMatchers::BooleanMatcher
- RSpec::Mocks::ArgumentMatchers::DuckTypeMatcher
- RSpec::Mocks::ArgumentMatchers::HashExcludingMatcher
- RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher
- RSpec::Mocks::ArgumentMatchers::InstanceOf
- RSpec::Mocks::ArgumentMatchers::KindOf
- RSpec::Mocks::ArgumentMatchers::NoArgsMatcher
- RSpec::Mocks::ArgumentMatchers::SingletonMatcher
- RSpec::Mocks::CallbackInvocationStrategy
- RSpec::Mocks::ClassNewMethodReference
- RSpec::Mocks::ClassVerifyingDouble
- RSpec::Mocks::Configuration
- RSpec::Mocks::Constant
- RSpec::Mocks::ConstantMutator
- RSpec::Mocks::ConstantMutator::BaseMutator
- RSpec::Mocks::ConstantMutator::ConstantHider
- RSpec::Mocks::ConstantMutator::DefinedConstantReplacer
- RSpec::Mocks::ConstantMutator::UndefinedConstantSetter
- RSpec::Mocks::DirectObjectReference
- RSpec::Mocks::Double
- RSpec::Mocks::ErrorGenerator
- RSpec::Mocks::ExampleMethods
- RSpec::Mocks::ExampleMethods::ExpectHost
- RSpec::Mocks::ExpectChain
- RSpec::Mocks::ExpectationTarget
- RSpec::Mocks::Implementation
- RSpec::Mocks::InstanceMethodReference
- RSpec::Mocks::InstanceMethodStasher
- RSpec::Mocks::InstanceVerifyingDouble
- RSpec::Mocks::MarshalExtension
- RSpec::Mocks::Matchers
- RSpec::Mocks::Matchers::ExpectationCustomization
- RSpec::Mocks::Matchers::HaveReceived
- RSpec::Mocks::Matchers::Receive
- RSpec::Mocks::Matchers::Receive::DefaultDescribable
- RSpec::Mocks::Matchers::ReceiveMessageChain
- RSpec::Mocks::Matchers::ReceiveMessages
- RSpec::Mocks::MessageChain
- RSpec::Mocks::MessageExpectation
- RSpec::Mocks::MessageExpectation::ImplementationDetails
- RSpec::Mocks::MethodDouble
- RSpec::Mocks::MethodReference
- RSpec::Mocks::Mutex
- RSpec::Mocks::NamedObjectReference
- RSpec::Mocks::NestedSpace
- RSpec::Mocks::NoCallbackInvocationStrategy
- RSpec::Mocks::ObjectMethodReference
- RSpec::Mocks::ObjectReference
- RSpec::Mocks::ObjectVerifyingDouble
- RSpec::Mocks::ObjectVerifyingDoubleMethods
- RSpec::Mocks::OrderGroup
- RSpec::Mocks::PartialClassDoubleProxy
- RSpec::Mocks::PartialClassDoubleProxyMethods
- RSpec::Mocks::PartialDoubleProxy
- RSpec::Mocks::Proxy
- RSpec::Mocks::ProxyForNil
- RSpec::Mocks::ReentrantMutex
- RSpec::Mocks::RootSpace
- RSpec::Mocks::SimpleMessageExpectation
- RSpec::Mocks::Space
- RSpec::Mocks::StubChain
- RSpec::Mocks::Syntax
- RSpec::Mocks::TargetBase
- RSpec::Mocks::TestDouble
- RSpec::Mocks::TestDoubleFormatter
- RSpec::Mocks::TestDoubleProxy
- RSpec::Mocks::VerifyingDouble
- RSpec::Mocks::VerifyingDouble::SilentIO
- RSpec::Mocks::VerifyingExistingClassNewMethodDouble
- RSpec::Mocks::VerifyingExistingMethodDouble
- RSpec::Mocks::VerifyingMessageExpectation
- RSpec::Mocks::VerifyingMethodDouble
- RSpec::Mocks::VerifyingPartialClassDoubleProxy
- RSpec::Mocks::VerifyingPartialDoubleProxy
- RSpec::Mocks::VerifyingProxy
- RSpec::Mocks::VerifyingProxyMethods
- RSpec::Mocks::Version
- BasicObject
- Class

No matching classes.

# 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

klass[R]

@private

message_chains[R]

@private

stubs[R]

@private

### Public Class Methods

new(klass) click to toggle source

    # File lib/rspec/mocks/any_instance/recorder.rb, line 16
    def initialize(klass)
      @message_chains = MessageChains.new
      @stubs = Hash.new { |hash, key| hash[key] = [] }
      @observed_methods = []
      @played_methods = {}
      @klass = klass
      @expectation_set = false
    end

### Public Instance Methods

already_observing?(method_name) click to toggle source

@private

    # File lib/rspec/mocks/any_instance/recorder.rb, line 121
    def already_observing?(method_name)
      @observed_methods.include?(method_name) || super_class_observing?(method_name)
    end

build_alias_method_name(method_name) click to toggle source

@private

    # File lib/rspec/mocks/any_instance/recorder.rb, line 116
    def build_alias_method_name(method_name)
      "__#{method_name}_without_any_instance__"
    end

expect_chain(*method_names_and_optional_return_values, &block) click to toggle source

@private

    # File lib/rspec/mocks/any_instance/recorder.rb, line 47
    def expect_chain(*method_names_and_optional_return_values, &block)
      @expectation_set = true
      normalize_chain(*method_names_and_optional_return_values) do |method_name, args|
        observe!(method_name)
        message_chains.add(method_name, ExpectChainChain.new(self, *args, &block))
      end
    end

instance_that_received(method_name) click to toggle source

@private

    # File lib/rspec/mocks/any_instance/recorder.rb, line 111
    def instance_that_received(method_name)
      @played_methods[method_name]
    end

notify_received_message(_object, message, args, _blk) click to toggle source

@private

    # File lib/rspec/mocks/any_instance/recorder.rb, line 126
    def notify_received_message(_object, message, args, _blk)
      has_expectation = false

      message_chains.each_unfulfilled_expectation_matching(message, *args) do |expectation|
        has_expectation = true
        expectation.expectation_fulfilled!
      end

      return unless has_expectation

      restore_method!(message)
      mark_invoked!(message)
    end

playback!(instance, method_name) click to toggle source

@private

    # File lib/rspec/mocks/any_instance/recorder.rb, line 103
    def playback!(instance, method_name)
      RSpec::Mocks.space.ensure_registered(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

should_not_receive(method_name, &block) click to toggle source

The opposite of `should_receive`

@see Methods#should_not_receive

    # File lib/rspec/mocks/any_instance/recorder.rb, line 69
    def should_not_receive(method_name, &block)
      should_receive(method_name, &block).never
    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 60
    def should_receive(method_name, &block)
      @expectation_set = true
      observe!(method_name)
      message_chains.add(method_name, PositiveExpectationChain.new(self, method_name, &block))
    end

stop_all_observation!() click to toggle source

@private

    # File lib/rspec/mocks/any_instance/recorder.rb, line 98
    def stop_all_observation!
      @observed_methods.each { |method_name| restore_method!(method_name) }
    end

stub(method_name, &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 29
    def stub(method_name, &block)
      observe!(method_name)
      message_chains.add(method_name, StubChain.new(self, method_name, &block))
    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 39
    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(self, *args, &block))
      end
    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 77
    def unstub(method_name)
      unless @observed_methods.include?(method_name.to_sym)
        AnyInstance.error_generator.raise_method_not_stubbed_error(method_name)
      end
      message_chains.remove_stub_chains_for!(method_name)
      stubs[method_name].clear
      stop_observing!(method_name) unless message_chains.has_expectation?(method_name)
    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 90
    def verify
      return unless @expectation_set
      return if message_chains.all_expectations_fulfilled?

      AnyInstance.error_generator.raise_second_instance_received_message_error(message_chains.unfulfilled_expectations)
    end

### Protected Instance Methods

stop_observing!(method_name) click to toggle source

    # File lib/rspec/mocks/any_instance/recorder.rb, line 142
    def stop_observing!(method_name)
      restore_method!(method_name)
      @observed_methods.delete(method_name)
      super_class_observers_for(method_name).each do |ancestor|
        ::RSpec::Mocks.space.
          any_instance_recorder_for(ancestor).stop_observing!(method_name)
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.