Class: RSpec::Mocks::ConstantMutator::DefinedConstantReplacer

### Home Classes Methods

### In Files

- lib/rspec/mocks/mutate_const.rb

### Parent

RSpec::Mocks::ConstantMutator::BaseMutator

### Methods

- ::new
- #mutate
- #reset
- #should_transfer_nested_constants?
- #to_constant
- #transfer_nested_constants
- #verify_constants_to_transfer!

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

Replaces a defined constant for the duration of an example.

@private

### Public Class Methods

new(*args) click to toggle source

    # File lib/rspec/mocks/mutate_const.rb, line 194
    def initialize(*args)
      super
      @constants_to_transfer = []
    end

### Public Instance Methods

mutate() click to toggle source

    # File lib/rspec/mocks/mutate_const.rb, line 199
    def mutate
      @context = recursive_const_get(@context_parts.join('::'))
      @original_value = get_const_defined_on(@context, @const_name)

      @constants_to_transfer = verify_constants_to_transfer!

      @context.__send__(:remove_const, @const_name)
      @context.const_set(@const_name, @mutated_value)

      transfer_nested_constants
    end

reset() click to toggle source

    # File lib/rspec/mocks/mutate_const.rb, line 219
    def reset
      @constants_to_transfer.each do |const|
        @mutated_value.__send__(:remove_const, const)
      end

      @context.__send__(:remove_const, @const_name)
      @context.const_set(@const_name, @original_value)
    end

should_transfer_nested_constants?() click to toggle source

    # File lib/rspec/mocks/mutate_const.rb, line 264
    def should_transfer_nested_constants?
      return true  if @transfer_nested_constants
      return false unless RSpec::Mocks.configuration.transfer_nested_constants?
      @original_value.respond_to?(:constants) && @mutated_value.respond_to?(:constants)
    end

to_constant() click to toggle source

    # File lib/rspec/mocks/mutate_const.rb, line 211
    def to_constant
      const = super
      const.stubbed = true
      const.previously_defined = true

      const
    end

transfer_nested_constants() click to toggle source

    # File lib/rspec/mocks/mutate_const.rb, line 228
    def transfer_nested_constants
      @constants_to_transfer.each do |const|
        @mutated_value.const_set(const, get_const_defined_on(original_value, const))
      end
    end

verify_constants_to_transfer!() click to toggle source

    # File lib/rspec/mocks/mutate_const.rb, line 234
    def verify_constants_to_transfer!
      return [] unless should_transfer_nested_constants?

      { @original_value => "the original value", @mutated_value => "the stubbed value" }.each do |value, description|
        next if value.respond_to?(:constants)

        raise ArgumentError,
              "Cannot transfer nested constants for #{@full_constant_name} "                    "since #{description} is not a class or module and only classes "                    "and modules support nested constants."
      end

      if Array === @transfer_nested_constants
        @transfer_nested_constants = @transfer_nested_constants.map(&:to_s) if RUBY_VERSION == '1.8.7'
        undefined_constants = @transfer_nested_constants - constants_defined_on(@original_value)

        if undefined_constants.any?
          available_constants = constants_defined_on(@original_value) - @transfer_nested_constants
          raise ArgumentError,
                "Cannot transfer nested constant(s) #{undefined_constants.join(' and ')} "                      "for #{@full_constant_name} since they are not defined. Did you mean "                      "#{available_constants.join(' or ')}?"
        end

        @transfer_nested_constants
      else
        constants_defined_on(@original_value)
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.