Sha256: 5b7e28fa87f06ffef37abccb928755b1cc29c00727518c3ebb69eb01b1853bcb

Contents?: true

Size: 1.28 KB

Versions: 18

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module ActiveSupport
  module Testing
    module ConstantStubbing
      # 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 <tt>World::List::Import::LARGE_IMPORT_THRESHOLD = 5000</tt> prevents
      # warnings from being thrown, and ensures that the old value is returned after the test has completed.
      #
      # 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.
      def stub_const(mod, constant, new_value)
        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
    end
  end
end

Version data entries

18 entries across 18 versions & 5 rubygems

Version Path
activesupport-7.1.5 lib/active_support/testing/constant_stubbing.rb
activesupport-7.1.4.2 lib/active_support/testing/constant_stubbing.rb
activesupport-7.1.4.1 lib/active_support/testing/constant_stubbing.rb
activesupport-7.1.4 lib/active_support/testing/constant_stubbing.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/testing/constant_stubbing.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/testing/constant_stubbing.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/testing/constant_stubbing.rb
activesupport-7.1.3.4 lib/active_support/testing/constant_stubbing.rb
activesupport-7.1.3.2 lib/active_support/testing/constant_stubbing.rb
activesupport-7.1.3.1 lib/active_support/testing/constant_stubbing.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3/lib/active_support/testing/constant_stubbing.rb
activesupport-7.1.3 lib/active_support/testing/constant_stubbing.rb
activesupport-7.1.2 lib/active_support/testing/constant_stubbing.rb
activesupport-7.1.1 lib/active_support/testing/constant_stubbing.rb
activesupport-7.1.0 lib/active_support/testing/constant_stubbing.rb
activesupport-7.1.0.rc2 lib/active_support/testing/constant_stubbing.rb
activesupport-7.1.0.rc1 lib/active_support/testing/constant_stubbing.rb
activesupport-7.1.0.beta1 lib/active_support/testing/constant_stubbing.rb