Class: RSpec::Mocks::ConstantMutator

### Home Classes Methods

### In Files

- lib/rspec/mocks/mutate_const.rb

### Parent

Object

### Namespace

- CLASS RSpec::Mocks::ConstantMutator::BaseMutator
- CLASS RSpec::Mocks::ConstantMutator::ConstantHider
- CLASS RSpec::Mocks::ConstantMutator::DefinedConstantReplacer
- CLASS RSpec::Mocks::ConstantMutator::UndefinedConstantSetter

### Methods

- ::hide
- ::mutate
- ::raise_on_invalid_const
- ::stub

### 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::ConstantMutator

Provides a means to stub constants.

### Public Class Methods

hide(constant_name) click to toggle source

Hides a constant.

@param (see ExampleMethods#hide_const)

@see ExampleMethods#hide_const @note It's recommended that you use `hide_const` in your

    examples. This is an alternate public API that is provided
    so you can hide constants in other contexts (e.g. helper
    classes).

    # File lib/rspec/mocks/mutate_const.rb, line 127
    def self.hide(constant_name)
      mutate(ConstantHider.new(constant_name, nil, {}))
      nil
    end

mutate(mutator) click to toggle source

Uses the mutator to mutate (stub or hide) a constant. Ensures that the mutator is correctly registered so it can be backed out at the end of the test.

@private

    # File lib/rspec/mocks/mutate_const.rb, line 316
    def self.mutate(mutator)
      ::RSpec::Mocks.space.register_constant_mutator(mutator)
      mutator.mutate
    end

raise_on_invalid_const() click to toggle source

Used internally by the constant stubbing to raise a helpful error when a constant like "A::B::C" is stubbed and A::B is not a module (and thus, it's impossible to define "A::B::C" since only modules can have nested constants).

@api private

    # File lib/rspec/mocks/mutate_const.rb, line 327
    def self.raise_on_invalid_const
      lambda do |const_name, failed_name|
        raise "Cannot stub constant #{failed_name} on #{const_name} "                  "since #{const_name} is not a module."
      end
    end

stub(constant_name, value, options={}) click to toggle source

Stubs a constant.

@param (see ExampleMethods#stub_const) @option (see ExampleMethods#stub_const) @return (see ExampleMethods#stub_const)

@see ExampleMethods#stub_const @note It's recommended that you use `stub_const` in your

    examples. This is an alternate public API that is provided
    so you can stub constants in other contexts (e.g. helper
    classes).

    # File lib/rspec/mocks/mutate_const.rb, line 107
    def self.stub(constant_name, value, options={})
      mutator = if recursive_const_defined?(constant_name, &raise_on_invalid_const)
                  DefinedConstantReplacer
                else
                  UndefinedConstantSetter
                end

      mutate(mutator.new(constant_name, value, options[:transfer_nested_constants]))
      value
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.