我想将我的测试与内化隔离开来。我使用 rails 3.2.8 和 rspec 2.11.1
我把这段代码放到 spec/support/translations.rb
module I18nHelpers
def with_translations(locale, translations)
I18n.backend.store_translations locale, translations
yield
ensure
I18n.reload!
end
结尾
RSpec.configure do |config|
config.include I18nHelpers
end
然后我测试应用程序助手:
describe ApplicationHelper do
context "messages" do
it "show body" do
with_translations :en, navigation: {messages: 'foo'} do
concat messages_navigation
assert_test 'span', 'foo'
end
end
end
end
但是这个测试伴随着消息
Failure/Error: assert_select 'span', text: /foo/
MiniTest::Assertion:
</foo/> expected but was
<"Messages">.
“消息”来自我的真实
config/locales/en.yml
我从控制台测试#store_translations 并且它有效。但是当我把线 p I18n.t(translations.key.first)
之前 ensure
帮助模块中的单词,它显示了旧的翻译。谢谢你的帮助!
最佳答案
与 I18n
完全隔离你可能想像这样切换后端:
def with_translations(locale, translations)
original_backend = I18n.backend
I18n.backend = I18n::Backend::KeyValue.new Hash.new, true
I18n.backend.store_translations locale, translations
yield
ensure
I18n.backend = original_backend
end
关于ruby-on-rails - rspec 中的 i18n 翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12963021/