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
- #instance_that_received
- #playback!
- #should_not_receive
- #should_receive
- #stop_all_observation!
- #stub
- #stub!
- #stub_chain
- #unstub
- #unstub!
- #verify

### Class/Module Index

Quicksearch

- RSpec
- RSpec::Mocks
- RSpec::Mocks::AllowanceTarget
- RSpec::Mocks::AmbiguousReturnError
- RSpec::Mocks::AndCallOriginalImplementation
- RSpec::Mocks::AndReturnImplementation
- RSpec::Mocks::AndYieldImplementation
- RSpec::Mocks::AnyInstance
- RSpec::Mocks::AnyInstance::Chain
- RSpec::Mocks::AnyInstance::Chain::Customizations
- RSpec::Mocks::AnyInstance::ExpectationChain
- RSpec::Mocks::AnyInstance::MessageChains
- RSpec::Mocks::AnyInstance::PositiveExpectationChain
- 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::BooleanMatcher
- RSpec::Mocks::ArgumentMatchers::DuckTypeMatcher
- RSpec::Mocks::ArgumentMatchers::EqualityProxy
- RSpec::Mocks::ArgumentMatchers::HashExcludingMatcher
- RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher
- RSpec::Mocks::ArgumentMatchers::InstanceOf
- RSpec::Mocks::ArgumentMatchers::KindOf
- RSpec::Mocks::ArgumentMatchers::MatcherMatcher
- RSpec::Mocks::ArgumentMatchers::NoArgsMatcher
- RSpec::Mocks::ArgumentMatchers::RegexpMatcher
- 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::ConstantStubber
- RSpec::Mocks::Deprecation
- RSpec::Mocks::ErrorGenerator
- RSpec::Mocks::ExampleMethods
- RSpec::Mocks::ExampleMethods::ExpectHost
- RSpec::Mocks::ExpectationTarget
- RSpec::Mocks::Implementation
- RSpec::Mocks::InstanceExec
- RSpec::Mocks::InstanceMethodStasher
- RSpec::Mocks::Matchers
- RSpec::Mocks::Matchers::HaveReceived
- RSpec::Mocks::Matchers::Receive
- RSpec::Mocks::Matchers::Receive::Customization
- RSpec::Mocks::MessageExpectation
- RSpec::Mocks::MethodDouble
- RSpec::Mocks::MethodDouble::ProcWithBlock
- RSpec::Mocks::Mock
- RSpec::Mocks::MockExpectationError
- RSpec::Mocks::OrderGroup
- RSpec::Mocks::Proxy
- RSpec::Mocks::ProxyForNil
- RSpec::Mocks::RecursiveConstMethods
- RSpec::Mocks::Space
- RSpec::Mocks::StubChain
- RSpec::Mocks::Syntax
- RSpec::Mocks::TargetBase
- RSpec::Mocks::TestDouble
- RSpec::Mocks::Version
- Marshal

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

message_chains[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
      @observed_methods = []
      @played_methods = {}
      @klass = klass
      @expectation_set = false
    end

### Public Instance Methods

already_observing?(method_name) click to toggle source

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

build_alias_method_name(method_name) click to toggle source

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

instance_that_received(method_name) click to toggle source

@private

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

playback!(instance, method_name) click to toggle source

@private

    # File lib/rspec/mocks/any_instance/recorder.rb, line 107
    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

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

stop_all_observation!() click to toggle source

@private

    # File lib/rspec/mocks/any_instance/recorder.rb, line 102
    def stop_all_observation!
      @observed_methods.each {|method_name| restore_method!(method_name)}
    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
    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

stub!(*) click to toggle source

@private

    # File lib/rspec/mocks/any_instance/recorder.rb, line 92
    def stub!(*)
      raise "stub! is not supported on any_instance. Use stub instead."
    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
    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

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 70
    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

unstub!(*) click to toggle source

@private

    # File lib/rspec/mocks/any_instance/recorder.rb, line 97
    def unstub!(*)
      raise "unstub! is not supported on any_instance. Use unstub instead."
    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 82
    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
    ensure
      stop_all_observation!
      ::RSpec::Mocks.space.remove_any_instance_recorder_for(@klass)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.