Skip to content

Commit

Permalink
Properly stub constants
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Feb 17, 2023
1 parent 42052e8 commit 288868e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
16 changes: 6 additions & 10 deletions test/i18n_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -501,18 +501,14 @@ def call(exception, locale, key, options); key; end

test "can reserve a key" do
begin
reserved_keys_were = I18n::RESERVED_KEYS.dup
stub_const(I18n, :RESERVED_KEYS, []) do
I18n.reserve_key(:foo)
I18n.reserve_key("bar")

assert !I18n::RESERVED_KEYS.include?(:foo)
assert !I18n::RESERVED_KEYS.include?(:bar)

I18n.reserve_key(:foo)
I18n.reserve_key("bar")

assert I18n::RESERVED_KEYS.include?(:foo)
assert I18n::RESERVED_KEYS.include?(:bar)
assert I18n::RESERVED_KEYS.include?(:foo)
assert I18n::RESERVED_KEYS.include?(:bar)
end
ensure
I18n::RESERVED_KEYS = reserved_keys_were
I18n.instance_variable_set(:@reserved_keys_pattern, nil)
end
end
Expand Down
10 changes: 10 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ def store_translations(locale, data, options = I18n::EMPTY_HASH)
def locales_dir
File.dirname(__FILE__) + '/test_data/locales'
end

def stub_const(klass, constant, new_value)
old_value = klass.const_get(constant)
klass.send(:remove_const, constant)
klass.const_set(constant, new_value)
yield
ensure
klass.send(:remove_const, constant)
klass.const_set(constant, old_value)
end
end

class DummyRackApp
Expand Down

0 comments on commit 288868e

Please sign in to comment.