How to test i18n in Rails with RSpec

Question context:

let's say that there is some really important line in config / locales / en.yml that is critical to existence.

en: foo: bar: "bubla!" 
  • I do not want to check every line, but
  • I don’t want the test to be too fragile (therefore there is no I18n.t ('foo.bar'). Should = ~ / bubla /)

so what I'm testing currently like this:

 #spec/locals_spec.rb require 'spec_helper' describe I18n do it do I18n.t('date.datepicker').should be_kind_of(String) end end 

this way I just guarantee that the translation exists and that it does not continue (for example, "foo.bar.car.lol"

but still I'm not satisfied

Question: What is the best practice for testing I18n translations with RSpec and where should I place them in the specifications folder?

+6
source share
2 answers

Check out this StackOverflow question for some ideas. My preferred way is this answer on the same question.

Update . Nowadays, I use i18n-tasks to handle tests related to i18n, not what I wrote above or previously answered StackOverflow.

I wanted to use i18n in my RSpec tests, primarily to make sure that I have translations for everything, i.e. there were no missed translations. i18n tasks can do this and much more through static analysis of my code, so I no longer need to run tests for all I18n.available_locales (in addition, when very locale-specific functions are tested, for example, switching from any locale to any other language in the system).

This meant that I can confirm that all i18n keys in the system really have values ​​(and that none of them are used or outdated), while keeping the number of repeated tests and, therefore, the set execution time down.

+4
source

I think I will write an acceptance test for such a "decisive" thing.

in most cases you need a translation in a certain context, i.e. display of something in a datepeck. I would check this context using capybara or something that works with the javascript driver.

just checking that this translation exists is useless unless you have the context in which it was used.

0
source

Source: https://habr.com/ru/post/919778/


All Articles