Module: SpecOompaLoompa::SpecHelpers

Defined in:
lib/spec_oompa_loompa/spec_helpers.rb

Instance Method Summary collapse

Instance Method Details

#with_constants(constants, &block) ⇒ Object

TODO : Write specs Easily alter constants within specs to test effects

Examples:

with_constants :RAILS_ENV  => 'custom_environment'  do
       TestBehaviour.in_custom_environment!
end

Parameters:

  • constants

    Hash A hash of constants and their values



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/spec_oompa_loompa/spec_helpers.rb', line 11

def with_constants(constants, &block)
  saved_constants = {}
  constants.each do |constant, val|
    parts = constant.to_s.split("::")
    konst_string = parts.pop
    konst_owner = parts.inject(Object){|k,p| k.const_get(p)}
    key = [konst_owner, konst_string]

    saved_constants[ key ] = konst_owner.const_get( konst_string )
    Kernel::silence_warnings { konst_owner.const_set( konst_string, val ) }
  end

  begin
    block.call
  ensure
    saved_constants.each do |(konst_owner, konst_string), orig_value|
      Kernel::silence_warnings { konst_owner.const_set( konst_string, orig_value ) }
    end
  end
end