Replaces a defined constant for the duration of an example.
@api private
# File lib/rspec/mocks/stub_const.rb, line 152 def previously_defined? true end
# File lib/rspec/mocks/stub_const.rb, line 156 def rspec_reset @context.send(:remove_const, @const_name) @context.const_set(@const_name, @original_value) end
# File lib/rspec/mocks/stub_const.rb, line 140 def stub @context = recursive_const_get(@context_parts.join('::')) @original_value = @context.const_get(@const_name) constants_to_transfer = verify_constants_to_transfer! @context.send(:remove_const, @const_name) @context.const_set(@const_name, @stubbed_value) transfer_nested_constants(constants_to_transfer) end
# File lib/rspec/mocks/stub_const.rb, line 161 def transfer_nested_constants(constants) constants.each do |const| @stubbed_value.const_set(const, original_value.const_get(const)) end end
# File lib/rspec/mocks/stub_const.rb, line 167 def verify_constants_to_transfer! return [] unless @transfer_nested_constants { @original_value => "the original value", @stubbed_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 - @original_value.constants if undefined_constants.any? available_constants = @original_value.constants - @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 @original_value.constants end end
Generated with the Darkfish Rdoc Generator 2.