Module: RSpec::Mocks

### Home Classes Methods

### In Files

- lib/rspec/mocks.rb
- lib/rspec/mocks/any_instance/chain.rb
- lib/rspec/mocks/any_instance/expect_chain_chain.rb
- lib/rspec/mocks/any_instance/expectation_chain.rb
- lib/rspec/mocks/any_instance/message_chains.rb
- lib/rspec/mocks/any_instance/proxy.rb
- lib/rspec/mocks/any_instance/recorder.rb
- lib/rspec/mocks/any_instance/stub_chain.rb
- lib/rspec/mocks/any_instance/stub_chain_chain.rb
- lib/rspec/mocks/argument_list_matcher.rb
- lib/rspec/mocks/argument_matchers.rb
- lib/rspec/mocks/configuration.rb
- lib/rspec/mocks/error_generator.rb
- lib/rspec/mocks/example_methods.rb
- lib/rspec/mocks/instance_method_stasher.rb
- lib/rspec/mocks/marshal_extension.rb
- lib/rspec/mocks/matchers/expectation_customization.rb
- lib/rspec/mocks/matchers/have_received.rb
- lib/rspec/mocks/matchers/receive.rb
- lib/rspec/mocks/matchers/receive_message_chain.rb
- lib/rspec/mocks/matchers/receive_messages.rb
- lib/rspec/mocks/message_chain.rb
- lib/rspec/mocks/message_expectation.rb
- lib/rspec/mocks/method_double.rb
- lib/rspec/mocks/method_reference.rb
- lib/rspec/mocks/mutate_const.rb
- lib/rspec/mocks/object_reference.rb
- lib/rspec/mocks/order_group.rb
- lib/rspec/mocks/proxy.rb
- lib/rspec/mocks/space.rb
- lib/rspec/mocks/syntax.rb
- lib/rspec/mocks/targets.rb
- lib/rspec/mocks/test_double.rb
- lib/rspec/mocks/verifying_double.rb
- lib/rspec/mocks/verifying_message_expecation.rb
- lib/rspec/mocks/verifying_proxy.rb
- lib/rspec/mocks/version.rb

### Namespace

- MODULE RSpec::Mocks::AnyInstance
- MODULE RSpec::Mocks::ArgumentMatchers
- MODULE RSpec::Mocks::ExampleMethods
- MODULE RSpec::Mocks::Matchers
- MODULE RSpec::Mocks::ObjectVerifyingDoubleMethods
- MODULE RSpec::Mocks::PartialClassDoubleProxyMethods
- MODULE RSpec::Mocks::Syntax
- MODULE RSpec::Mocks::TestDouble
- MODULE RSpec::Mocks::VerifyingDouble
- MODULE RSpec::Mocks::VerifyingProxyMethods
- MODULE RSpec::Mocks::Version
- CLASS RSpec::Mocks::AllowanceTarget
- CLASS RSpec::Mocks::AndReturnImplementation
- CLASS RSpec::Mocks::AndWrapOriginalImplementation
- CLASS RSpec::Mocks::AndYieldImplementation
- CLASS RSpec::Mocks::AnyInstanceAllowanceTarget
- CLASS RSpec::Mocks::AnyInstanceExpectationTarget
- CLASS RSpec::Mocks::ArgumentListMatcher
- CLASS RSpec::Mocks::ClassVerifyingDouble
- CLASS RSpec::Mocks::Configuration
- CLASS RSpec::Mocks::Constant
- CLASS RSpec::Mocks::ConstantMutator
- CLASS RSpec::Mocks::DirectObjectReference
- CLASS RSpec::Mocks::Double
- CLASS RSpec::Mocks::ErrorGenerator
- CLASS RSpec::Mocks::ExpectChain
- CLASS RSpec::Mocks::ExpectationTarget
- CLASS RSpec::Mocks::Implementation
- CLASS RSpec::Mocks::InsertOntoBacktrace
- CLASS RSpec::Mocks::InstanceMethodReference
- CLASS RSpec::Mocks::InstanceMethodStasher
- CLASS RSpec::Mocks::InstanceVerifyingDouble
- CLASS RSpec::Mocks::MarshalExtension
- CLASS RSpec::Mocks::MessageChain
- CLASS RSpec::Mocks::MessageExpectation
- CLASS RSpec::Mocks::MethodDouble
- CLASS RSpec::Mocks::MethodReference
- CLASS RSpec::Mocks::NamedObjectReference
- CLASS RSpec::Mocks::NestedSpace
- CLASS RSpec::Mocks::ObjectMethodReference
- CLASS RSpec::Mocks::ObjectReference
- CLASS RSpec::Mocks::ObjectVerifyingDouble
- CLASS RSpec::Mocks::OrderGroup
- CLASS RSpec::Mocks::PartialClassDoubleProxy
- CLASS RSpec::Mocks::PartialDoubleProxy
- CLASS RSpec::Mocks::Proxy
- CLASS RSpec::Mocks::ProxyForNil
- CLASS RSpec::Mocks::RootSpace
- CLASS RSpec::Mocks::SimpleMessageExpectation
- CLASS RSpec::Mocks::Space
- CLASS RSpec::Mocks::StubChain
- CLASS RSpec::Mocks::TargetBase
- CLASS RSpec::Mocks::TestDoubleProxy
- CLASS RSpec::Mocks::VerifyingExistingMethodDouble
- CLASS RSpec::Mocks::VerifyingMessageExpectation
- CLASS RSpec::Mocks::VerifyingMethodDouble
- CLASS RSpec::Mocks::VerifyingPartialClassDoubleProxy
- CLASS RSpec::Mocks::VerifyingPartialDoubleProxy
- CLASS RSpec::Mocks::VerifyingProxy

### Methods

- ::allow_message
- ::configuration
- ::expect_message
- ::setup
- ::teardown
- ::verify
- ::with_temporary_scope

### 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::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::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::InsertOntoBacktrace
- 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::NamedObjectReference
- RSpec::Mocks::NestedSpace
- 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::RootSpace
- RSpec::Mocks::SimpleMessageExpectation
- RSpec::Mocks::Space
- RSpec::Mocks::Space::FakeMutex
- RSpec::Mocks::StubChain
- RSpec::Mocks::Syntax
- RSpec::Mocks::TargetBase
- RSpec::Mocks::TestDouble
- RSpec::Mocks::TestDoubleProxy
- RSpec::Mocks::VerifyingDouble
- 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

Contains top-level utility methods. While this contains a few public methods, these are not generally meant to be called from a test or example. They exist primarily for integration with test frameworks (such as rspec-core).

### Constants

CannotSupportArgMutationsError

  Raised for situations that RSpec cannot support due to mutations made externally on arguments that RSpec is holding onto to use for later comparisons.

  @deprecated We no longer raise this error but the constant remains until

      RSpec 4 for SemVer reasons.

ExpiredTestDoubleError

  Raised when a test double is used after it has been torn down (typically at the end of an rspec-core example).

IGNORED_BACKTRACE_LINE

  @private

MockExpectationAlreadyInvokedError

  Raised when an expectation customization method (e.g. `with`, `and_return`) is called on a message expectation which has already been invoked.

MockExpectationError

  Raised when a message expectation is not satisfied.

NegationUnsupportedError

  @private

OutsideOfExampleError

  Raised when doubles or partial doubles are used outside of the per-test lifecycle.

UnsupportedMatcherError

  @private

VerifyingDoubleNotDefinedError

  @private

### Attributes

space[R]

@private

### Public Class Methods

allow_message(subject, message, opts={}, &block) click to toggle source

Adds an allowance (stub) on `subject`

@param subject the subject to which the message will be added @param message a symbol, representing the message that will be

    added.

@param opts a hash of options, :expected_from is used to set the

    original call site

@yield an optional implementation for the allowance

@example Defines the implementation of `foo` on `bar`, using the passed block

    x = 0
    RSpec::Mocks.allow_message(bar, :foo) { x += 1 }

    # File lib/rspec/mocks.rb, line 69
    def self.allow_message(subject, message, opts={}, &block)
      space.proxy_for(subject).add_stub(message, opts, &block)
    end

configuration() click to toggle source

Mocks specific configuration, as distinct from `RSpec.configuration` which is core RSpec configuration.

    # File lib/rspec/mocks/configuration.rb, line 181
    def self.configuration
      @configuration ||= Configuration.new
    end

expect_message(subject, message, opts={}, &block) click to toggle source

Sets a message expectation on `subject`. @param subject the subject on which the message will be expected @param message a symbol, representing the message that will be

    expected.

@param opts a hash of options, :expected_from is used to set the

    original call site

@yield an optional implementation for the expectation

@example Expect the message `foo` to receive `bar`, then call it

    RSpec::Mocks.expect_message(bar, :foo)
    bar.foo

    # File lib/rspec/mocks.rb, line 84
    def self.expect_message(subject, message, opts={}, &block)
      space.proxy_for(subject).add_message_expectation(message, opts, &block)
    end

setup() click to toggle source

Performs per-test/example setup. This should be called before an test or example begins.

    # File lib/rspec/mocks.rb, line 38
    def self.setup
      @space_stack << (@space = space.new_scope)
    end

teardown() click to toggle source

Cleans up all test double state (including any methods that were redefined on partial doubles). This must be called after each example, even if an error was raised during the example.

    # File lib/rspec/mocks.rb, line 51
    def self.teardown
      space.reset_all
      @space_stack.pop
      @space = @space_stack.last || @root_space
    end

verify() click to toggle source

Verifies any message expectations that were set during the test or example. This should be called at the end of an example.

    # File lib/rspec/mocks.rb, line 44
    def self.verify
      space.verify_all
    end

with_temporary_scope() click to toggle source

Call the passed block and verify mocks after it has executed. This allows mock usage in arbitrary places, such as a `before(:all)` hook.

    # File lib/rspec/mocks.rb, line 90
    def self.with_temporary_scope
      setup

      begin
        yield
        verify
      ensure
        teardown
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.