Rails i18n规范不起作用

时间:2015-01-22 13:51:47

标签: ruby-on-rails rspec ruby-on-rails-3.2

在我的rails应用程序中,我使用gem i18n_spec添加rspec测试,检查我的i18n配置文件是否正确。

规范文件

require 'spec_helper.rb'
require 'i18n-spec/tasks'

Dir.glob('config/locales/*.yml') do |locale_file|
  describe "#{locale_file}" do
    it { subject.should be_parseable }

导致错误

  ../gems/i18n-spec-0.6.0/lib/i18n-spec/tasks.rb:5:in `<top (required)>': undefined method `namespace' for main:Object (NoMethodError)
    from ../gems/backports-3.3.3/lib/backports/tools.rb:328:in `require'
    from ../gems/backports-3.3.3/lib/backports/tools.rb:328:in `require_with_backports'
    from ../gems/activesupport-3.2.21/lib/active_support/dependencies.rb:251:in `block in require'
    from ../gems/activesupport-3.2.21/lib/active_support/dependencies.rb:236:in `load_dependency'
    from ../gems/activesupport-3.2.21/lib/active_support/dependencies.rb:251:in `require'
    from ../spec/selftest/i18n_spec.rb:3:in `<top (required)>'

1 个答案:

答案 0 :(得分:2)

为什么要测试它们是否可解析?如果你在一个单元/功能规范中断言任何 I18n键,Rails将加载你的I18n文件并在yml文件不可解析时抛出语法错误!

你会得到这样的断言:

  within(".mod-header .login") { click_link I18n.t('public.menu.login') }
  find("#main-container h1").should have_content I18n.t('pages.login.title')

这样Rails就会加载并解析你的yml文件。如果有任何错误,您将在测试输出中找到它们!

这样的错误:

<Psych::SyntaxError: (your_project_path/config/locales/nl.yml): mapping values are not allowed in this context at line <num> column <num>>

<Psych::SyntaxError: (your_project_path/config/locales/nl.yml): did not find expected key while parsing a block mapping at <num> column <num>>