Module: ActiveSupport::Testing::ConstantStubbing

Included in:
ActiveSupport::TestCase
Defined in:
lib/active_support/testing/constant_stubbing.rb

Instance Method Summary collapse

Instance Method Details

#stub_const(mod, constant, new_value, exists: true) ⇒ Object

Changes the value of a constant for the duration of a block. Example:

# World::List::Import::LARGE_IMPORT_THRESHOLD = 5000
stub_const(World::List::Import, :LARGE_IMPORT_THRESHOLD, 1) do
  assert_equal 1, World::List::Import::LARGE_IMPORT_THRESHOLD
end

assert_equal 5000, World::List::Import::LARGE_IMPORT_THRESHOLD

Using this method rather than forcing World::List::Import::LARGE_IMPORT_THRESHOLD = 5000 prevents warnings from being thrown, and ensures that the old value is returned after the test has completed.

If the constant doesn’t already exists, but you need it set for the duration of the block you can do so by passing ‘exists: false`.

stub_const(object, :SOME_CONST, 1, exists: false) do
  assert_equal 1, SOME_CONST
end

Note: Stubbing a const will stub it across all threads. So if you have concurrent threads (like separate test suites running in parallel) that all depend on the same constant, it’s possible divergent stubbing will trample on each other.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/active_support/testing/constant_stubbing.rb', line 28

def stub_const(mod, constant, new_value, exists: true)
  if exists
    begin
      old_value = mod.const_get(constant, false)
      mod.send(:remove_const, constant)
      mod.const_set(constant, new_value)
      yield
    ensure
      mod.send(:remove_const, constant)
      mod.const_set(constant, old_value)
    end
  else
    if mod.const_defined?(constant)
      raise NameError, "already defined constant #{constant} in #{mod.name}"
    end

    begin
      mod.const_set(constant, new_value)
      yield
    ensure
      mod.send(:remove_const, constant)
    end
  end
end