Module: ConstConf::ConstConfHelper

Defined in:
lib/const_conf/spec/const_conf_helper.rb

Overview

Sets constants and their configured values for testing purposes.

This module provides a helper method for temporarily overriding constant values during testing, including nested constants within modules. It ensures that specified constants exist before stubbing them and handles configuration of boolean-check methods for nested constants.

Examples:

const_conf_as('FooConfig::DATABASE::ENABLED' => true)

Instance Method Summary collapse

Instance Method Details

#const_conf_as(config_hash) ⇒ Object

Sets constants and their configured values for testing purposes.

This method is designed to facilitate testing by allowing test code to temporarily override the values of constants, including nested constants within modules. It ensures that the specified constants exist before attempting to stub them, and also handles the configuration of boolean-check methods for nested constants.

their intended values

Parameters:

  • config_hash (Hash)

    a hash mapping constant names (as strings) to



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/const_conf/spec/const_conf_helper.rb', line 21

def const_conf_as(config_hash)
  config_hash.each do |const_name, value|
    Object.const_defined?(const_name) or
      raise NameError, "constant #{const_name} does not exist"
    stub_const("#{const_name}", value)
    const_parts = const_name.split('::')
    if const_parts.size > 1
      parent_const_name = const_parts[0..-2] * '::'
      Object.const_defined?(parent_const_name) or raise NameError,
        "parent constant #{parent_const_name} does not exist"
      parent_const = Object.const_get(parent_const_name)
      allow(parent_const).to receive("#{const_parts.last}?").
        and_return(value)
    end
  end
end