Class: RSpec::Mocks::ConstantStubber

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

- ::find
- ::hide
- ::mutate
- ::mutators
- ::raise_on_invalid_const
- ::register_mutator
- ::reset_all
- ::stub

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

Provides a means to stub constants.

### Public Class Methods

find(name) click to toggle source

    # File lib/rspec/mocks/mutate_const.rb, line 379
    def self.find(name)
      mutators.find { |s| s.full_constant_name == name }
    end

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 189
    def self.hide(constant_name)
      return unless recursive_const_defined?(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.

@api private

    # File lib/rspec/mocks/mutate_const.rb, line 348
    def self.mutate(mutator)
      register_mutator(mutator)
      mutator.mutate
    end

mutators() click to toggle source

The list of constant mutators that have been used for the current example.

@api private

    # File lib/rspec/mocks/mutate_const.rb, line 370
    def self.mutators
      @mutators ||= []
    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 389
    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

register_mutator(mutator) click to toggle source

@api private

    # File lib/rspec/mocks/mutate_const.rb, line 375
    def self.register_mutator(mutator)
      mutators << mutator
    end

reset_all() click to toggle source

Resets all stubbed constants. This is called automatically by rspec-mocks when an example finishes.

@api private

    # File lib/rspec/mocks/mutate_const.rb, line 357
    def self.reset_all
      # We use reverse order so that if the same constant
      # was stubbed multiple times, the original value gets
      # properly restored.
      mutators.reverse.each { |s| s.rspec_reset }

      mutators.clear
    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 169
    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.