class RSpec::Mocks::ConstantMutator::DefinedConstantReplacer
Replaces a defined constant for the duration of an example.
@api private
Public Instance Methods
mutate()
click to toggle source
# File lib/rspec/mocks/mutate_const.rb, line 248 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(constants_to_transfer) end
rspec_reset()
click to toggle source
# File lib/rspec/mocks/mutate_const.rb, line 268 def rspec_reset @context.__send__(:remove_const, @const_name) @context.const_set(@const_name, @original_value) end
to_constant()
click to toggle source
Calls superclass method
RSpec::Mocks::ConstantMutator::BaseMutator#to_constant
# File lib/rspec/mocks/mutate_const.rb, line 260 def to_constant const = super const.stubbed = true const.previously_defined = true const end
transfer_nested_constants(constants)
click to toggle source
# File lib/rspec/mocks/mutate_const.rb, line 273 def transfer_nested_constants(constants) constants.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 279 def verify_constants_to_transfer! return [] unless @transfer_nested_constants { @original_value => "the original value", @mutated_value => "the stubbed value" }.each do |value, description| unless 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 end if @transfer_nested_constants.is_a?(Array) @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