简体   繁体   中英

Rails i18n spec doesn't work

In my rails app, I use the gem i18n_spec to add rspec tests that check if my i18n config files are correct.

The spec file

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 }

results in errors

  ../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)>'

Why do you want to test if they are parseable? If you are asserting any I18n key in a unit / feature spec Rails will load your I18n files and throw a syntax error if the yml files aren't parseable!

You will get assertions like so:

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

This way Rails will load and parse your yml files. And if there are any errors you will find them in your test output!

And errors like this:

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

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM